Rediscovering the Obvious

…stumbling in the footsteps of greatness

DataTriggers are VERY cool

without comments

I’ve been reading a lot about Triggers in WPF styles, but I didn’t have much call to use them yet since we’re still working on getting a UI out there and databound for a decent slice of the app… we haven’t really started looking at nifty UI behavioral effects yet, and Triggers only work against DependencyProperty objects.

Well, thanks to this useful article, I discovered the joy of the DataTrigger, which can target any old .NET object (PODNO – Plain old dot net object?)  I’d previously blogged this code for auto-expansion of a full tree view:

            <TreeView.ItemContainerStyle>
                <Style>
                    <Setter Property=”TreeViewItem.IsExpanded” Value=”True”/>
                </Style>
            </TreeView.ItemContainerStyle>

It turns out that adding a data trigger to that same style enabled me to perform the default selection in the tree node, which was otherwise proving to be quite a challenge writing code like (_orgSelectionTree.ItemContainerGenerator.ContainerFromItem(value) as TreeViewItem).IsSelected = true; That code, unfortunately, doesn’t work until the TreeViewItem corresponding to value is already rendered at least once, causing even MORE problems.

            <TreeView.ItemContainerStyle>
                <Style>
                    <Setter Property=”TreeViewItem.IsExpanded” Value=”True”/>
                    <Style.Triggers>
                        <DataTrigger Binding=”{Binding Path=IsPrimaryUserOrganization}” Value=”True”>
                            <Setter Property=”TreeViewItem.IsSelected” Value=”True”/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TreeView.ItemContainerStyle>
 

I need to get better at adding search engine terms for these posts, but luckily the code samples will get me TreeView and TreeViewItem in this case.
 

Written by erwilleke

April 25th, 2007 at 9:52 am

Posted in Uncategorized

Leave a Reply