I'm trying to set the DataContext of several toggle buttons, each of which will correspond to a particular element of a list. These toggle buttons are static, not dynamically generated, because we wanted to group them in the layout, and not have them all in one area.
ListOfRoles
is a collection of objects, each of which has an IsSelected
property (type bool?
) and a name. The nameToObject
converter returns the object with the name given in the converter.
My problem is that XAML is trying to bind IsSelected
before it has evaluated the DataContext binding, which throws an exception on our system. It tries to bind to the object which RoleContainerStyle
is applied to, and crashes. Here is the XAML:
<Style x:Key="RoleContainerStyle" TargetType="{x:Type MyControls:MyListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MyControls:MyListBoxItem}">
<Grid>
<StackPanel Orientation="Horizontal" >
<ToggleButton Content="Driver To Scene" IsChecked="{Binding IsSelected}" DataContext="{Binding ListOfRoles, Converter={StaticResource nameToObject}, ConverterParameter='Driver To'}" HorizontalAlignment="Stretch" Margin="0" Width="80" Height="40" FontSize="14.667" />
<ToggleButton Content="Driver From Scene" IsChecked="{Binding IsSelected}" DataContext="{Binding ListOfRoles, Converter={StaticResource nameToObject}, ConverterParameter='Driver From'}" HorizontalAlignment="Stretch" Margin="8,0,0,0" Width="80" Height="40" FontSize="14.667" />
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I put breakpoints in the converter and verified that it DOES NOT enter the converter before trying to evaluate the IsSelected
binding.
If I remove the IsSelected
binding, I AM able to trap execution within the converter, so it seems like a problem with order of evaluation, unless I am missing something about XAML.
I have tried changing the order of the properties in XAML, tried using the long form of specifying Bindings (nested tags), and I am just out of ideas.
Thanks in advance.