如何绑定WPF用户控件';是否将DataGrid工具提示FontSize设置为在我的主XAML窗口中定义的变量?

如何绑定WPF用户控件';是否将DataGrid工具提示FontSize设置为在我的主XAML窗口中定义的变量?,wpf,xaml,data-binding,datagrid,Wpf,Xaml,Data Binding,Datagrid,我有一份WPF申请。 我正在尝试为工具提示设置FontSize和Background的DataGrid中的项目 我定义了以下XAML片段: <DataGridTextColumn Binding="{Binding Foo}" Header="Foo" Visibility="Visible" Wid

我有一份WPF申请。 我正在尝试为
工具提示设置
FontSize
Background
DataGrid
中的项目

我定义了以下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>
在运行时,我收到以下错误:

System.Windows.Data错误:4:找不到与绑定的源 参考'ElementName=MyWindow'。BindingExpression:Path=TBackground; DataItem=null;目标元素是“工具提示”(名称=“”);目标属性 是“背景”(类型为“画笔”)

System.Windows.Data错误:4:找不到与绑定的源 参考'ElementName=MyWindow'。BindingExpression:Path=TFontSize; DataItem=null;目标元素是“工具提示”(名称=“”);目标属性 是“FontSize”(类型为“Double”)

我在这里错过了什么绑定过程


谢谢属性的
t字体大小应为
double
类型,并返回有效的字体大小>0:

private double _tFontSize = 20;
public double TFontSize
{
    get { return _tFontSize; }
    set
    {
        _tFontSize = value;
        NotifyPropertyChanged("TFontSize");
    }
}
然后可以将
DataGridCell
标记
属性绑定到窗口,然后通过
工具提示
放置目标
属性将
字体大小
背景
属性绑定到窗口的源属性:

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


由于
工具提示
位于其自身的可视树中,因此您无法使用
元素名称
直接绑定到窗口

您的XAML中是否确实有名为
MyWindow
的元素?这是我的主XAML窗口的名称。也许像这样,还没有测试过,不确定它是否能工作。Background=“{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGrid},Path=DataContext.TBackground}”因为您的DataGrid的DataContext将是我的窗口,您可以访问它的属性TBackground…我想MM8:它仍然不在那里。工具提示用于包含在用户控件中的datagrid;fontsize/background是我的主窗口上的一个属性。我收到以下错误:System.Windows.Data错误:4:找不到引用为“ElementName=MyMainWindow”的绑定源。BindingExpression:(无路径);DataItem=null;目标元素是“DataGridCell”(名称=“”);目标属性为“标记”(类型为“对象”)请尝试将标记属性设置为相对资源。我编辑了我的答案。mm8:哇。我想我还没有完全理解绑定。我想我可以使用主窗口名导航到正确的位置。你能解释一下为什么你的改变有效吗?谢谢。
<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>