【问题标题】:Binding a custom AttachedProperty to a ToolTip - Bug?将自定义 AttachedProperty 绑定到工具提示 - 错误?
【发布时间】:2012-07-13 07:32:49
【问题描述】:

我想将自定义 AttachedProperty 的值绑定到 ToolTip 的内容。

绑定有效,但仅在工具提示的第二次打开时有效。当工具提示第一次打开时,绑定有它的 FallbackValue。

奇怪的是,它适用于“默认”附加属性,例如 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=&quot;It doesn't work&quot;}" />
        </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);
    }
}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

编辑 1:解决方案和新问题:

当我在工具提示中再次定义命名空间“本地”时,我发现它有效:

<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=&quot;It doesn't work&quot;}" />
            </ToolTip>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>

    <Button local:AttachedProperty.TestProperty="Now it works!" Content="Button" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>

【问题讨论】:

    标签: wpf xaml tooltip attached-properties


    【解决方案1】:

    我在 PropertyChangedCallback 中添加了一些跟踪输出(并删除了 Inherits 标志,因为它在这里没用):

    public static readonly DependencyProperty TestPropertyProperty =
        DependencyProperty.RegisterAttached(
            "TestProperty",
            typeof(string),
            typeof(AttachedProperty),
            new FrameworkPropertyMetadata(
                string.Empty,
                (o, e) => Trace.TraceInformation("Setting TestProperty = \"{1}\" on {0}", o, e.NewValue))
    );
    

    Visual Studio 输出证明该属性在绑定抱怨找不到之前已设置:

    ToolTipAttachedProperty.vshost.exe Information: 0 : Setting TestProperty = "Now it works!" on System.Windows.Controls.Button
    System.Windows.Data Warning: 40 : BindingExpression path error: '(local:AttachedProperty.TestProperty)' property not found on 'object' ''Button' (Name='')'. BindingExpression:Path=(local:AttachedProperty.TestProperty); DataItem='Button' (Name=''); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
    

    您或许应该将此发送给 Microsoft。

    编辑:参考您关于将工具提示放入样式的编辑。你可以这样写:

    <Grid.Resources>
        <ToolTip xmlns:local="clr-namespace:Test" x:Key="ButtonToolTip"
                 DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}">
            <TextBlock Text="{Binding (local:AttachedProperty.TestProperty), FallbackValue=&quot;It doesn't work&quot;}" />
        </ToolTip>
        <Style TargetType="Button">
            <Setter Property="ToolTip" Value="{StaticResource ButtonToolTip}"/>
        </Style>
    </Grid.Resources>
    

    【讨论】:

    • 不,我不能。观察到的行为真的很奇怪,最让我困惑的是绑定第二次起作用了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 2011-09-28
    相关资源
    最近更新 更多