Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果未设置,则从一个DependencyProperty回退到另一个DependencyProperty_C#_Wpf_User Controls_Dependency Properties - Fatal编程技术网

C# 如果未设置,则从一个DependencyProperty回退到另一个DependencyProperty

C# 如果未设置,则从一个DependencyProperty回退到另一个DependencyProperty,c#,wpf,user-controls,dependency-properties,C#,Wpf,User Controls,Dependency Properties,我想要什么: 我创建了一个UserControlImageButton,可以在WPF中设置图像。通常,除了一个状态外,我会对所有状态重用默认映像,但必须以非常详细的方式配置控件(请参见下面截取的WPF)。我想实现的是,每个未设置的图像都只使用默认图像 我所拥有的: 这就是我目前描述ImageButton的方式: <cc:ImageButton x:Name="cmdHideCustomWindowingArea" DefaultImage="/Images

我想要什么:

我创建了一个UserControl
ImageButton
,可以在WPF中设置图像。通常,除了一个状态外,我会对所有状态重用默认映像,但必须以非常详细的方式配置控件(请参见下面截取的WPF)。我想实现的是,每个未设置的图像都只使用默认图像

我所拥有的:

这就是我目前描述
ImageButton
的方式:

<cc:ImageButton x:Name="cmdHideCustomWindowingArea" 
                DefaultImage="/Images/UI/ButtonDefault.png"
                HoverImage="/Images/UI/ButtonDefault.png"
                DownImage="/Images/UI/ButtonActive.png"
                UpImage="/Images/UI/ButtonDefault.png"
                DisabledImage="/Images/UI/ButtonDefault.png"/>
到目前为止我所尝试的:

1。我已经尝试过检查图像是否设置为这样,这不起作用,代码似乎根本就没有到达getter

get { return (ImageSource)GetValue(HoverImageProperty) ?? DefaultImage; }
2.此外,像建议的那样设置
TargetNullValue
也不起作用(“无法识别或访问成员“TargetNullValue”):

但这将返回以下错误:

无法在“Binding”类型的“Path”属性上设置“Binding”。只能对DependencyObject的DependencyProperty设置“绑定”

我必须做的事情:


如果我要查找的属性未设置,那么返回到另一个
DependencyProperty
的正确方法是什么?

听起来好像您正在查找(尽管您以前尝试过使用它)。在链接页面中,一个
优先绑定

描述附加到单个绑定目标属性的绑定对象集合,该属性从成功生成值的集合中的第一个绑定接收其值

PriorityBinding允许将绑定目标(target)属性与绑定列表相关联。成功返回值的第一个绑定将成为活动绑定

如果满足以下条件,绑定将成功返回值:

1.绑定源的路径解析成功

2.值转换器(如果有)能够转换结果值

3.结果值对目标属性有效

您应该在
PriorityBinding
s中重新排列
绑定路径
。。。请尝试以下方法:

<PriorityBinding>
    <Binding Path="HoverImage" RelativeSource="{RelativeSource TemplatedParent}" />
    <Binding Path="DefaultImage" RelativeSource="{RelativeSource TemplatedParent}" />
</PriorityBinding>

有关更多信息,您还可以在Tech Pro网站上阅读本教程


更新>>>


正如您正确指出的,
PriorityBinding
确实会接受
null
作为有效的返回值。但是,您可以通过使用一个自定义实现来解决这个问题,该实现在实际值为
null

时返回
dependencProperty.UnsetValue
,谢谢您的建议。这实际上工作得更好一些,不再抛出错误,但现在按钮“闪烁”。我认为“null”值可能被视为成功,因此buttn正在“null”和默认图像之间切换。。。(因为没有加载图像时它没有大小,所以鼠标不会悬停在它上面,所以它会返回默认值,依此类推)
get { return (ImageSource)GetValue(HoverImageProperty) ?? DefaultImage; }
<Trigger Property="IsPressed" Value="true">
    <Setter TargetName="ButtonImage" Property="Source" 
            Value="{Binding DownImage, RelativeSource={RelativeSource TemplatedParent}}" 
            TargetNullValue="{Binding DefaultImage, RelativeSource={RelativeSource TemplatedParent}}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
    <Setter TargetName="ButtonImage" Property="Source">
        <Setter.Value>
            <PriorityBinding>
                <Binding Path="{Binding HoverImage, RelativeSource={RelativeSource TemplatedParent}}"/>
                <Binding Path="{Binding DefaultImage, RelativeSource={RelativeSource TemplatedParent}}"/>
            </PriorityBinding>
        </Setter.Value>
    </Setter> 
</Trigger>
<PriorityBinding>
    <Binding Path="HoverImage" RelativeSource="{RelativeSource TemplatedParent}" />
    <Binding Path="DefaultImage" RelativeSource="{RelativeSource TemplatedParent}" />
</PriorityBinding>