I've got a simple user control that contains an image whose source I want to change based on a property in the parent (which could be another UC or a Window). A simplified version of the UC looks like this
<UserControl x:Class="Test.Controls.DualStateButton" ... x:Name="root">
<Grid>
<Image Height="{Binding Height, ElementName=root}" Stretch="Fill" Width="{Binding Width, ElementName=root}">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="{Binding ImageOff, ElementName=root}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding State}" Value="True">
<Setter Property="Source" Value="{Binding ImageOn, ElementName=root}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</Grid>
</UserControl>
Height, Width, ImageOff, ImageOn, and State are all dependency properties on the UC. The UC has no DataContext set, so it should be inheriting the parent. What I'm trying to do is something like the following where the State in the UC is bound to the DualState property of the Window.
<Window x:Class="Test.MainWindow" DataContext="{Binding RelativeSource={RelativeSource Self}}">
...
<Grid>
<local:DualStateButton State="{Binding DualState}" Height="100" ImageOff="{StaticResource ButtonUp}" ImageOn="{StaticResource ButtonDown}" Width="100"/>
</Grid>
</Window>
What I get, however, is a error saying that 'State' property not found on 'object' ''MainWindow', so it appears to be taking the binding 'State' in the UC literally and not assigning it to the DualState property of the Window. Can someone shed some insight on what I'm doing wrong?
If I set the State property on the UC either through code or XAML (as a bool value) it works fine. The State DP is defined as follows.
public static readonly DependencyProperty StateProperty =
DependencyProperty.Register("State", typeof(bool), typeof(DualStateButton),
new PropertyMetadata(false));
public bool State
{
get { return (bool)GetValue(StateProperty); }
set { SetValue(StateProperty, value); }
}
Does it data type need to be a binding or something in order for this to work?