从ItemsControl中的每个项的属性绑定到ItemsControl外部的对象的属性

时间:2015-04-07 23:44:44

标签: c# wpf data-binding dependency-properties itemscontrol

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

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

我试图通过数据触发器,但没有运气:

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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

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}}"/>