我有一個WPF應用程序。 我試圖在DataGrid
中的某個項目上設置的FontSize
和Background
。如何將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')
我在這裏錯過了綁定過程?
感謝
你實際上在你的XAML中有一個名爲'MyWindow'的元素嗎? –
這是我的主要xaml窗口的名稱。 –
JohnB
也許是這樣,未經測試,不知道它是否會工作。我的數據網格的數據上下文將是MyWindow,就像你可以訪問它的屬性TBackground ...我的數據上下文...我的數據網格的DataContext將是MyWindow,並且你可以訪問它的屬性TBackground ...我認爲 –