I have the following XAML:
<TextBlock Text='{Binding ElementName=EditListBox, Path=SelectedItems.Count}' Margin='0,0,5,0'/> <TextBlock Text='items selected'> <TextBlock.Style> <Style TargetType='{x:Type TextBlock}'> <Style.Triggers> <DataTrigger Binding='{Binding ElementName=EditListBox, Path=SelectedItems.Count}' Value='1'> <Setter Property='TextBlock.Text' Value='item selected'></Setter> </DataTrigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock>
The first text block happily changes with SelectedItems.Count, showing 0,1,2, etc. The datatrigger on the second block never seems to fire to change the text.
Any thoughts?
The DataTrigger is firing but the Text field for your second TextBlock is hard-coded as ‘items selected’ so it won’t be able to change. To see it firing, you can remove Text=’items selected’.
Your problem is a good candidate for using a ValueConverter instead of DataTrigger. Here’s how to create and use the ValueConverter to get it to set the Text to what you want.
Create this ValueConverter:
Add the namespace reference to your the assembly the converter is located:
Add the converter to your resources:
Change your second textblock to: