2012-07-13 37 views
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=&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); 
    } 
} 

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=&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> 

回答

2

我已經添加在PropertyChangedCallback一些跟蹤輸出(和刪除Inherits標誌,因爲它是我們eless這裏):

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') 

你或許應該送這對微軟。

編輯:引用您關於將工具提示放在樣式中的編輯。你可以這樣寫:

<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> 
+0

非常感謝。有用!你能解釋爲什麼嗎? – Jens 2012-07-13 09:28:14

+0

不,我不能。觀察到的行爲真的很奇怪,最讓我困惑的是綁定第二次起作用。 – Clemens 2012-07-13 09:35:07