【问题标题】:Binding from a property of each item inside an ItemsControl to a property of an object outside the ItemsControl从 ItemsControl 内的每个项目的属性绑定到 ItemsControl 外的对象的属性
【发布时间】:2015-04-07 23:44:43
【问题描述】:

我有一个 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);
        }
    }
}

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 我希望按钮的 IsEnabled 属性在我更改其中一项中的 MyProperty 属性时更改。我已经测试了您的解决方案(通过稍微修复它),不幸的是 IsEnabled 属性仅在应用程序启动时更新一次。
    猜你喜欢
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 2011-06-16
    • 1970-01-01
    相关资源
    最近更新 更多