C# 如何绑定工具提示';是否将内容添加到代码中的MVVM属性?

C# 如何绑定工具提示';是否将内容添加到代码中的MVVM属性?,c#,.net,wpf,silverlight,C#,.net,Wpf,Silverlight,由于我们软件的性质,我们必须在代码隐藏中动态创建datagrid列,然后将其添加到datagrid,如下所示: DataGridBoundColumn dataGridBoundColumn = new DataGridTextColumn { CellSty

由于我们软件的性质,我们必须在代码隐藏中动态创建datagrid列,然后将其添加到datagrid,如下所示:

DataGridBoundColumn dataGridBoundColumn = new DataGridTextColumn
                                                          {
                                                              CellStyle = ...,                                                                            
                                                              Header = header,
                                                              Binding = binding
                                                          };
reportDataGrid.Columns.Add(dataGridBoundColumn);
现在我们需要一个关于columnheader的工具提示:

ToolTipService.SetToolTip(dataGridBoundColumn, "ENTER VALUE");
那也行。但是,我需要将工具提示的值绑定到ViewModel上的属性。我知道如何在xaml中做到这一点,但不知道如何在代码中做到这一点

任何帮助都将不胜感激

更新:

多亏了史蒂夫的回答,我才能够以稍微不同的方式解决这个问题:

Binding bindingBoundToTooltipProperty = new Binding()
                                   {
                                       Source = reportDataGrid.DataContext, 
                                       Path = new PropertyPath("ToolTipSorting")
                                   };


BindingOperations.SetBinding(dataGridBoundColumn, ToolTipService.ToolTipProperty, bindingBoundToTooltipProperty);
如果DataGridColumnHeaderStyle是自定义的,请确保也将以下行添加到模板中:

<Trigger Property="IsMouseOver" Value="True">
    <Setter Property="ToolTip" Value="{Binding Column.(ToolTipService.ToolTip), RelativeSource={RelativeSource Self}}"/>
</Trigger>

您应该能够如下设置绑定:

BindingOperations.SetBinding(dataGridBoundColumn,
    ToolTipService.ToolTipProperty,
    new Binding("Path.To.My.Property"));
注意:此字段的
DataContext
将是列上
标题
属性的值

要绑定到视图模型上的属性;假设您的视图模型是
DataGrid
DataContext
,您可能希望将绑定更改为如下内容:

new Binding("DataContext.ToolTipSorting")
{
    RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor)
    {
        AncestorType = typeof(DataGrid)
    }
}

这将尝试定位类型为
DataGrid
的第一个父对象,并获取其
DataContext.ToolTipSorting
属性的值。

您应该能够设置绑定,如下所示:

BindingOperations.SetBinding(dataGridBoundColumn,
    ToolTipService.ToolTipProperty,
    new Binding("Path.To.My.Property"));
注意:此字段的
DataContext
将是列上
标题
属性的值

要绑定到视图模型上的属性;假设您的视图模型是
DataGrid
DataContext
,您可能希望将绑定更改为如下内容:

new Binding("DataContext.ToolTipSorting")
{
    RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor)
    {
        AncestorType = typeof(DataGrid)
    }
}

这将尝试定位类型为
DataGrid
的第一个父对象,并获取其
DataContext.ToolTipSorting
属性的值。

。我获取System.Windows.Data错误:2:找不到目标元素的治理FrameworkElement或FrameworkContentElement。BindingExpression:Path=ToolTipSorting;DataItem=null;目标元素是“DataGridTextColumn”(HashCode=42996073);目标属性为“ToolTip”(类型为“Object”)如果Datacontext位于标头上,我如何绑定到ViewModel上的MVVM属性“ToolTipSorting”呢?我不能100%确定
Datacontext
是否为
标头
属性-您可以将绑定路径留空以查看示例中的数据上下文是什么类型的吗?抱歉,忽略我最后的评论-被误解了。见我的最新消息,非常感谢你,史蒂夫。它并不是那样工作的,但是你给了我正确的方向。我对我的问题进行了编辑,只做了一点小小的改动,以使它起作用。再次感谢您的帮助!谢谢你,史蒂夫。我获取System.Windows.Data错误:2:找不到目标元素的治理FrameworkElement或FrameworkContentElement。BindingExpression:Path=ToolTipSorting;DataItem=null;目标元素是“DataGridTextColumn”(HashCode=42996073);目标属性为“ToolTip”(类型为“Object”)如果Datacontext位于标头上,我如何绑定到ViewModel上的MVVM属性“ToolTipSorting”呢?我不能100%确定
Datacontext
是否为
标头
属性-您可以将绑定路径留空以查看示例中的数据上下文是什么类型的吗?抱歉,忽略我最后的评论-被误解了。见我的最新消息,非常感谢你,史蒂夫。它并不是那样工作的,但是你给了我正确的方向。我对我的问题进行了编辑,只做了一点小小的改动,以使它起作用。再次感谢您的帮助!