I have the following XAML:
<UserControl.Resources>
<DataTemplate x:Key="ExpanderTemplate">
<Grid>
<Rectangle Stroke="Black" StrokeThickness="1" Width="10" Height="10" Fill="White" />
<Rectangle Stroke="Black" StrokeThickness="1" Width="6" Height="1" Fill="Black" />
<Rectangle Stroke="Black" StrokeThickness="3" Width="2" Height="6" Fill="Black" />
</Grid>
</DataTemplate>
<DataTemplate x:Key="CollapserTemplate">
<Grid>
<Rectangle Stroke="Black" StrokeThickness="1" Width="10" Height="10" Fill="White" />
<Rectangle Stroke="Black" StrokeThickness="1" Width="6" Height="1" Fill="Black" />
</Grid>
</DataTemplate>
</UserControl.Resources>
<Grid>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
<Grid>
<ContentPresenter x:Name="ExpanderPresenter" ContentTemplate="{StaticResource ExpanderTemplate}" Visibility="{Binding ExpanderVisibility}" />
<ContentPresenter x:Name="CollapserPresenter" ContentTemplate="{StaticResource CollapserTemplate}" Visibility="{Binding CollapserVisibility}" />
</Grid>
</StackPanel>
</Grid>
As you can see, it's essentially a textblock with its contents bound to a name, and two contentpresenters which have their visibility bound to a couple of Visibility objects on the source class. The source class looks like this:
public class MyViewModel
{
public string Name { get; set; }
public Visibility CollapserVisibility
{
get
{
if (IsExpandable && IsExpanded)
{
return Visibility.Visible;
}
else
{
return Visibility.Collapsed;
}
}
}
public Visibility ExpanderVisibility
{
get
{
if (IsExpandable && !IsExpanded)
{
return Visibility.Visible;
}
else
{
return Visibility.Collapsed;
}
}
}
public bool IsExpanded { get; set; }
public bool IsExpandable { get; set; }
}
The problem I am experiencing is that the bindings with the Visibility objects never even happen. The binding with the Name string does happen, and I have verified that (both visually and by expanding out and putting a breakpoint in the getter), but when I put breakpoints in the getters of the CollapserVisibility and ExpanderVisibility objects, those breakpoints never even get hit. Why is this? I'm also not getting any binding errors in the output window of Visual Studio, which further confuses me, so it acts as if the binding was set up correctly.
Am I not allowed to bind the Visibility property of a ContentPresenter? I also tried moving the Visibility bindings to be on the "Grid" objects in the data templates (in the XAML), but to no avail...it didn't work.
What's wrong with my binding? Thanks for any help.