【发布时间】:2011-11-09 17:01:12
【问题描述】:
由于我们软件的性质,我们必须在后面的代码中动态创建我们的数据网格列,然后将其添加到数据网格中,如下所示:
DataGridBoundColumn dataGridBoundColumn = new DataGridTextColumn
{
CellStyle = ...,
Header = header,
Binding = binding
};
reportDataGrid.Columns.Add(dataGridBoundColumn);
现在我们需要在列标题上添加一个工具提示:
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>
【问题讨论】:
标签: c# .net wpf silverlight