WPF: setting datatemplate using binding property
I am very new to WPF. I have a ListBox where I want to set a template for an item (I named it Person). A TextBlock must be used. The person must have public properties: "Foreground" and "IsOnline". If "IsOnline" is true, I would use the "Foreground" property of the person, otherwise "Gray". All this. My first template:
<DataTemplate x:Key="UnselectedPersonTemplate" DataType="{x:Type o:Person}">
<TextBlock Text="{Binding Path=Name}" Foreground="{Binding Path=Foreground}" Margin="1">
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=IsOnline}" Value="False">
<Setter Property="TextBlock.Foreground" Value="Gray" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Of course it doesn't work. Not an exception, but also not an expected result. Then I tried using Trigger for the TextBlock, but there will be an exception that I cannot use DataTriggers (EventTriggers only) to do this.
Thanks for any advice! :)
0
Enrico
a source
to share
1 answer
Give your TextBlock
name and set the set property to t12 for that name.
<DataTemplate x:Key="UnselectedPersonTemplate" DataType="{x:Type o:Person}">
<TextBlock x:Name="tb" Text="{Binding Path=Name}"
Foreground="{Binding Path=Foreground}" Margin="1" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=IsOnline}" Value="False">
<Setter TargetName="tb" Property="Foreground" Value="Gray" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
+3
a source to share