2017-04-10 30 views
1

我有一個WPF應用程序。 我試圖在DataGrid中的某個項目上設置的FontSizeBackground如何將WPF UserControl的DataGrid ToolTip FontSize綁定到我的主XAML窗口中定義的變量?

我有以下XAML代碼片段定義:

<DataGridTextColumn Binding="{Binding Foo}" 
           Header="Foo" 
           Visibility="Visible" 
           Width="*"> 
    <DataGridTextColumn.CellStyle> 
     <Style TargetType="DataGridCell"> 
     <Setter Property="ToolTip" > 
      <Setter.Value> 
       <ToolTip Background="{Binding ElementName=MyWindow,Path=TBackground}" 
         FontSize="{Binding ElementName=MyWindow,Path=TFontSize}" > 
        <TextBlock Text="{Binding Foo}" /> 
       </ToolTip> 
      </Setter.Value> 
     </Setter> 
     </Style> 
    </DataGridTextColumn.CellStyle> 
</DataGridTextColumn> 

我有以下的代碼中定義的背後 「mywindow的」

private Brush _tBackground; 
public Brush TBackground 
{ 
    get { return _tBackground; } 
    set 
    { 
     _tBackground = value; 
     NotifyPropertyChanged("TBackground"); 
    } 
} 

private int _tFontSize; 
public int TFontSize 
{ 
    get { return _tFontSize; } 
    set 
    { 
     _tFontSize = value; 
     NotifyPropertyChanged("TFontSize"); 
    } 
} 

在運行時,我得到以下錯誤:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=MyWindow'. BindingExpression:Path=TBackground; DataItem=null; target element is 'ToolTip' (Name=''); target property is 'Background' (type 'Brush')

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=MyWindow'. BindingExpression:Path=TFontSize; DataItem=null; target element is 'ToolTip' (Name=''); target property is 'FontSize' (type 'Double')

我在這裏錯過了綁定過程?

感謝

+0

你實際上在你的XAML中有一個名爲'MyWindow'的元素嗎? –

+0

這是我的主要xaml窗口的名稱。 JohnB

+0

也許是這樣,未經測試,不知道它是否會工作。我的數據網格的數據上下文將是MyWindow,就像你可以訪問它的屬性TBackground ...我的數據上下文...我的數據網格的DataContext將是MyWindow,並且你可以訪問它的屬性TBackground ...我認爲 –

回答

1

TFontSize屬性應該是double類型,並返回一個有效的字體大小> 0:

private double _tFontSize = 20; 
public double TFontSize 
{ 
    get { return _tFontSize; } 
    set 
    { 
     _tFontSize = value; 
     NotifyPropertyChanged("TFontSize"); 
    } 
} 

然後,您可以在DataGridCellTag屬性綁定到窗口,然後綁定FontSizeBackground屬性到窗口的源屬性通過PlacementTarget屬性的Tooltip

<DataGridTextColumn.CellStyle> 
    <Style TargetType="DataGridCell"> 
     <Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource AncestorType=Window}}" /> 
     <Setter Property="ToolTip" > 
      <Setter.Value> 
       <ToolTip 
        Background="{Binding Path=PlacementTarget.Tag.TBackground, RelativeSource={RelativeSource Self}}" 
        FontSize="{Binding Path=PlacementTarget.Tag.TFontSize, RelativeSource={RelativeSource Self}}" > 
        <TextBlock Text="{Binding Foo}" /> 
       </ToolTip> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</DataGridTextColumn.CellStyle> 

由於Tooltip位於其自己的可視化樹中,因此無法使用ElementName直接綁定到窗口。

+0

mm8:現在還不夠。工具提示適用於包含在用戶控件中的數據網格;字體大小/背景是我的主窗口上的屬性。我得到以下錯誤:System.Windows.Data錯誤:4:找不到與引用'ElementName = MyMainWindow'綁定的源。 BindingExpression :(無路徑);的DataItem = NULL;目標元素是'DataGridCell'(Name ='');目標屬性是'Tag'(輸入'Object') – JohnB

+0

嘗試將Tag屬性設置爲RelativeSource。我編輯了我的答案。 – mm8

+0

mm8:哇。我想我還沒有完全理解綁定。我想通過使用主窗口名稱可以導航到正確的位置。你能解釋一下爲什麼你的改變起作用了嗎?謝謝。 – JohnB

相關問題