3
我想将自定义AttachedProperty的值绑定到ToolTip的内容。将自定义AttachedProperty绑定到工具提示 - Bug?
该绑定工作,但只在工具提示的第二个打开。第一次打开工具提示时,绑定具有其FallbackValue。
奇怪的是,它适用于“Default”AttachedProperties,例如Grid.Row。
任何人都可以解释吗?
的代码非常简单:
<Button local:AttachedProperty.TestProperty="Now it works!" Content="Button">
<Button.ToolTip>
<ToolTip DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}">
<TextBlock Text="{Binding (local:AttachedProperty.TestProperty), FallbackValue="It doesn't work"}" />
</ToolTip>
</Button.ToolTip>
</Button>
的AttachedProperty的代码:
public static class AttachedProperty
{
public static readonly DependencyProperty TestPropertyProperty = DependencyProperty.RegisterAttached
(
"TestProperty",
typeof(string),
typeof(AttachedProperty),
new FrameworkPropertyMetadata
(
string.Empty,
FrameworkPropertyMetadataOptions.Inherits
)
);
public static string GetTestProperty(FrameworkElement target)
{
return (string)target.GetValue(TestPropertyProperty);
}
public static void SetTestProperty(FrameworkElement target, string value)
{
target.SetValue(TestPropertyProperty, value);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
编辑1:解决方案和一个新的问题:
我发现当它再次定义名称空间“local”时,它就可以工作工具提示:
<ToolTip xmlns:local="clr-namespace:Test" DataContext=...
但
如果要想要一个风格中做到这一点,你的错误
"XMLNamespace", "Assembly" or "ClrNamespace" not found in Mapping Expression
的XML的代码为我的新的测试项目是:
<Grid x:Name="LayoutRoot">
<Grid.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip xmlns:local="clr-namespace:Test" DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}" >
<TextBlock Text="{Binding (local:AttachedProperty.TestProperty), FallbackValue="It doesn't work"}" />
</ToolTip>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Button local:AttachedProperty.TestProperty="Now it works!" Content="Button" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
非常感谢。有用!你能解释为什么吗? – Jens 2012-07-13 09:28:14
不,我不能。观察到的行为真的很奇怪,最让我困惑的是绑定第二次起作用。 – Clemens 2012-07-13 09:35:07