Using data triggers
Property triggers work with dependency properties only, but what about regular properties? This is where data triggers come in. They are able to inspect any property, but their usefulness lies within data templates that naturally bind to data objects (which utilize non-dependency properties). Let's see how to set that up.
Getting ready
Make sure Visual Studio is up and running.
How to do it...
We'll create a simple application to show books with a DataTemplate
that is customized with data triggers to show some books a bit differently:
Create a new WPF application named
CH08.DataTriggerDemo
.Add a new class to the project named
Book
and implement it as follows:class Book { public string BookName { get; set; } public string AuthorName { get; set; } public bool IsFree { get; set; } }
Open
MainWindow.xaml
. Add aListBox
to the existingGrid
and set a few properties:<ListBox HorizontalContentAlignment="Stretch" ItemsSource="{Binding}">
We'll create a
DataTemplate...