如何将 DataTrigger 绑定到 Interface 属性

发布于 2024-11-16 10:18:41 字数 611 浏览 0 评论 0原文

我有 4 个类实现我的自定义 ICalendarItem 接口。 该界面有一个名为“Jours”的属性。

ObservableCollection<KeyValuePair<DateTime, DateTime>> Jours;

我的类像这样覆盖该属性:

public override ObservableCollection<KeyValuePair<DateTime, DateTime>> Jours {...}

当 Jours.Count 从 0 变为 1 时,我想触发一个操作,所以我尝试了以下操作:

<DataTrigger Binding="{Binding Path=Jours.Count}" Value="1">

<DataTrigger Binding="{Binding Path=(ICalendarItem)Jours.Count}" Value="1">

这 2 个 DataTrigger 都不起作用。

有人知道如何将 DataTrigger 绑定到 Interface 属性吗?

I have 4 class which implement my custom ICalendarItem Interface.
That interface has a property called 'Jours'.

ObservableCollection<KeyValuePair<DateTime, DateTime>> Jours;

My class override that property like this :

public override ObservableCollection<KeyValuePair<DateTime, DateTime>> Jours {...}

When the Jours.Count goes from 0 to 1, i want to trigger an action so i tried this :

<DataTrigger Binding="{Binding Path=Jours.Count}" Value="1">

<DataTrigger Binding="{Binding Path=(ICalendarItem)Jours.Count}" Value="1">

None of these 2 DataTrigger works.

Anyone know how to bind a DataTrigger to an Interface property?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海未深 2024-11-23 10:18:41

当您想要专门绑定到自定义接口属性时,需要将命名空间、接口和属性名称放在括号内。然后,您可以在括号外引用子属性,例如 Count。

<DataTrigger Binding="{Binding Path=(local:ICalendarItem.Jours).Count}" Value="1">
...
</DataTrigger>

When you want to specifically bind to a custom interface property, you need to place place the namespace, interface and property name within parenthesis. You can then reference a sub-property like Count outside of the parenthesis.

<DataTrigger Binding="{Binding Path=(local:ICalendarItem.Jours).Count}" Value="1">
...
</DataTrigger>
夏见 2024-11-23 10:18:41

在我的测试中,它的工作做得很好。请参考下面的代码,或许对你有帮助。

此代码的作用是,当“Jours.Count”等于“3”时,窗口背景变为红色。
XAML:

<Window x:Class="WpfApplication1.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 TargetType="Grid">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Jours.Count}" Value="3">
                    <Setter Property="Control.Background" Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
    </Grid>
</Window>

隐藏代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        ITest test = new TestClass();
        this.DataContext = test;
    }
}

interface ITest
{
    ObservableCollection<KeyValuePair<DateTime, DateTime>> Jours { get; set; }
}

class TestClass : ITest
{
    public TestClass()
    {
        Jours = new ObservableCollection<KeyValuePair<DateTime, DateTime>>();
        Jours.Add(new KeyValuePair<DateTime, DateTime>(DateTime.Now, DateTime.Now));
        Jours.Add(new KeyValuePair<DateTime, DateTime>(DateTime.Now, DateTime.Now));
        Jours.Add(new KeyValuePair<DateTime, DateTime>(DateTime.Now, DateTime.Now));
    }

    public ObservableCollection<KeyValuePair<DateTime, DateTime>> Jours { get; set; }
}

In my test, its work is done well. Please refer to the following code, which probably helps with you.

What this code do is, when `Jours.Count' equals to "3", the Window background gets red color.
XAML:

<Window x:Class="WpfApplication1.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 TargetType="Grid">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Jours.Count}" Value="3">
                    <Setter Property="Control.Background" Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
    </Grid>
</Window>

CodeBehind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        ITest test = new TestClass();
        this.DataContext = test;
    }
}

interface ITest
{
    ObservableCollection<KeyValuePair<DateTime, DateTime>> Jours { get; set; }
}

class TestClass : ITest
{
    public TestClass()
    {
        Jours = new ObservableCollection<KeyValuePair<DateTime, DateTime>>();
        Jours.Add(new KeyValuePair<DateTime, DateTime>(DateTime.Now, DateTime.Now));
        Jours.Add(new KeyValuePair<DateTime, DateTime>(DateTime.Now, DateTime.Now));
        Jours.Add(new KeyValuePair<DateTime, DateTime>(DateTime.Now, DateTime.Now));
    }

    public ObservableCollection<KeyValuePair<DateTime, DateTime>> Jours { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文