首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将WPF用户控件的DataGrid ToolTip FontSize绑定到主XAML窗口中定义的变量?

如何将WPF用户控件的DataGrid ToolTip FontSize绑定到主XAML窗口中定义的变量?
EN

Stack Overflow用户
提问于 2017-04-10 22:18:24
回答 1查看 466关注 0票数 1

我有一份WPF申请。我正在尝试将FontSizeBackground设置为DataGrid中的项目上的ToolTip

I定义了以下XAML片段:

代码语言:javascript
复制
<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"后面的代码中定义了以下内容

代码语言:javascript
复制
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错误:4:无法找到引用“ElementName=MyWindow”绑定的源代码。BindingExpression:Path=TBackground;DataItem=null;目标元素为'ToolTip‘(名称=’‘);目标属性为’背景‘(键入'Brush') System.Windows.Data错误:4:无法找到引用“ElementName=MyWindow”绑定的源代码。BindingExpression:Path=TFontSize;DataItem=null;目标元素是'ToolTip‘(Name='');目标属性是'FontSize’(键入'Double')

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

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-11 08:49:50

TFontSize属性应该是double类型,并返回一个有效的字体大小> 0:

代码语言:javascript
复制
private double _tFontSize = 20;
public double TFontSize
{
    get { return _tFontSize; }
    set
    {
        _tFontSize = value;
        NotifyPropertyChanged("TFontSize");
    }
}

然后,可以将DataGridCellDataGridCell属性绑定到窗口,然后通过TooltipPlacementTarget属性将FontSizeBackground属性绑定到窗口的源属性。

代码语言:javascript
复制
<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直接绑定到窗口。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43333767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档