How to bind DataTrigger to an Interface property
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="{Bin开发者_JAVA技巧ding Path=(ICalendarItem)Jours.Count}" Value="1">
None of these 2 DataTrigger works.
Anyone know how to bind a DataTrigger to an Interface property?
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>
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; }
}
精彩评论