我有一个包含图片我想改变基于父属性(也可以是另一个UC或窗口),其源一个简单的用户控制。在UC的简化版本看起来是这样的
<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>
Run Code Online (Sandbox Code Playgroud)
高度,宽度,ImageOff,ImageOn和State都是UC上的依赖项属性。UC没有设置DataContext,因此它应该继承父级。我正在试图做的是一样的东西,其中在UC国家势必窗口DualState属性如下。
<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>
Run Code Online (Sandbox Code Playgroud)
我能得到什么,但是,是一个错误,说在“对象”“”主窗口没有发现“国家”财产”,所以它似乎正在结合‘国家’在UC字面上,而不是将其分配到的DualState财产窗户。有人能阐明什么,我做错了一些见解?
如果我设置在UC的国有资产既可以通过代码或XAML(为bool值),它工作正常。国家DP定义如下。
public static readonly DependencyProperty StateProperty =
DependencyProperty.Register("State", typeof(bool), typeof(DualStateButton),
new PropertyMetadata(false));
public bool State
{
get { return (bool)GetValue(StateProperty); …Run Code Online (Sandbox Code Playgroud)