【发布时间】:2014-06-10 12:44:50
【问题描述】:
我有一个我在 WPF 中编写的自定义控件,它有一个布尔依赖属性:
public static readonly DependencyProperty IsAlertProperty
= DependencyProperty.Register("IsAlert", typeof(bool), typeof(AlertControl),
new FrameworkPropertyMetadata(default(bool),
FrameworkPropertyMetadataOptions.None,
OnIsAlertChanged,
null,
false,
UpdateSourceTrigger.PropertyChanged));
public bool IsAlert
{
get { return (bool)GetValue(IsAlertProperty); }
set { SetValue(IsAlertProperty, value); }
}
在我的 Generic.xaml 中,我有以下 xaml 代码:
<Style TargetType="{x:Type local:AlertControl}">
<Setter Property="Template">
<Setter.Value> ... </Setter.Value>
</Setter>
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip Visiblity="{Binding Path=IsAlert,
RelativeSource={RelativeSource TemplatedParent},
Converter={StaticResource BooleanToVisiblityConverter}">
<!-- Tooltip content goes here -->
</ToolTip>
</Setter.Value>
</Setter>
<Style />
问题是这似乎不起作用。我使用 Snoop 监视我的 xaml,IsAlert 属性正在适当更改,但如果我深入研究我的AlertControl.ToolTip,我发现Visiblity DependencyProperty 有绑定错误。我也尝试使用RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:AlertControl}},但这也带来了绑定问题。我不知道如何诊断这个,因为我的输出窗口也没有吐出任何绑定表达式错误。
【问题讨论】:
标签: c# wpf xaml custom-controls dependency-properties