繁体   English   中英

从ItemsControl内每个项目的属性绑定到ItemsControl外的对象的属性

[英]Binding from a property of each item inside an ItemsControl to a property of an object outside the ItemsControl

我有一个ItemsControl,以及ItemsControl之外的一个Button。 ItemsControl中的每个项目都有一个名为“ MyProperty”的依赖项属性(在后面的代码中定义)。

当ItemsControl中的至少一个项目的MyProperty属性设置为5时,我想将Button的IsEnabled属性设置为false。(当然,这只是更复杂情况的愚蠢示例)

我尝试通过数据触发器进行尝试,但是没有运气:

XAML:

<Window x:Class="cancellami24.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <Style x:Key="MyStyle" TargetType="{x:Type ContentPresenter}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=MyProperty}" Value="5">
                    <Setter Property="IsEnabled" TargetName="MyButton" Value="False" /><!--error on TargetName-->
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <ItemsControl x:Name="MyListBox" Grid.Row="0" ItemContainerStyle="{StaticResource MyStyle}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=MyProperty, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

        <Button x:Name="MyButton" Grid.Row="1" Click="MyButton_Click"/>
    </Grid>
</Window>

后台代码:

using System.Collections.ObjectModel;
using System.Windows;

namespace cancellami24
{
    public partial class MainWindow : Window
    {
        private readonly ObservableCollection<MyItem> myCollection = new ObservableCollection<MyItem>();

        public MainWindow()
        {
            InitializeComponent();

            myCollection.Add(new MyItem(1));
            myCollection.Add(new MyItem(2));
            myCollection.Add(new MyItem(3));
            MyListBox.ItemsSource = myCollection;
        }

        private void MyButton_Click(object sender, RoutedEventArgs e)
        {
            myCollection[2].SetValue(MyItem.MyPropertyProperty, 5);
        }
    }

    public class MyItem : DependencyObject
    {
        public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(int), typeof(MyItem));

        public MyItem(int propertyValue)
        {
            SetValue(MyPropertyProperty, propertyValue);
        }
    }
}

您需要自定义转换器来解决它

public class MyConverter : IValueConverter
{
    bool flag = false;
    var collection = value as ObservableCollection<MyItem>();
    if(collection==null) return flag;
    foreach (var item in collection)
    {
        if (item.MyProperty==5)
        {
            flag = true;
            break;
        }
    }
    return flag;
} 

将MyConverter添加到您的App.xaml

<local:MyConverter x:key="MyConverter"/>

Xaml:

<Button x:Name="MyButton" IsEnabled="{Binding ElementName=MyListBox, Path=ItemsSource, Converter={StaticResource MyConverter}}"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM