wpf 将 DataGridCell ToolTip 属性绑定到 DataGridCell 的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27418403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Binding DataGridCell ToolTip property to value of DataGridCell
提问by monstr
I have DataGridand one of the DataGridcolumns looks like this
我有DataGrid,其中一DataGrid列看起来像这样
<DataGridTextColumn Header="Value"
Binding="{Binding Value, Converter={StaticResource BooleanToYesNoConverter}}"
x:Name="_col2"
IsReadOnly="True"
CanUserSort="False"
Width="*">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="ToolTip" Value="{Binding Value, Converter={StaticResource BooleanToYesNoConverter}}" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
The problem is I forced to use BooleanToYesNoConverterconverter twice. It means that Convertmethod of BooleanToYesNoConverterwill be invoked twice. Therefore, I want to optimize my code. And want to bind value of ToolTipproperty directly to value of cell.
问题是我被迫使用BooleanToYesNoConverter转换器两次。这意味着该Convert方法BooleanToYesNoConverter将被调用两次。因此,我想优化我的代码。并希望将ToolTip属性值直接绑定到单元格的值。
I tried approach with using ElementName-s. But I have no idea what should I specify in Pathproperty of binding.
我尝试使用ElementName-s 的方法。但是我不知道应该在绑定的Path属性中指定什么。
<DataGridTextColumn Header="Value"
Binding="{Binding Value, Converter={StaticResource BooleanToYesNoConverter}}"
x:Name="_col2"
IsReadOnly="True"
CanUserSort="False"
Width="*">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="ToolTip" Value="{Binding ElementName=_col2, Path=???}" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
I tried to use DataGridTemplateColumninstead of DataGridTextColumn, but it does't work too.
我尝试使用DataGridTemplateColumn而不是DataGridTextColumn,但它也不起作用。
<DataGridTemplateColumn CanUserSort="False"
Header="Значение"
Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value, Converter={StaticResource BooleanToYesNoConverter}}"
Name="_textBlock"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="ToolTip" Value="{Binding RelativeSource ElementName=_textBlock, Path=Text}" />
</Style>
</DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>
How can I solve my task. Is it possible at all?
我该如何解决我的任务。有可能吗?
回答by Amol Bavannavar
Use this Style :
使用这种风格:
<Style TargetType="DataGridCell">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},Path=Content.Text}"/>
</Style>
回答by Xtr
Try just setting the ToolTip to the DataGridCell's DataContext like so:
尝试将 ToolTip 设置为 DataGridCell 的 DataContext,如下所示:
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="ToolTip" Value="{Binding}" />
</Style>
</DataGridTextColumn.CellStyle>
If you do not get the desired content you can then specify the converter as well:
如果您没有获得所需的内容,您也可以指定转换器:
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="ToolTip" Value="{Binding Converter={StaticResource BooleanToYesNoConverter}}" />
</Style>
</DataGridTextColumn.CellStyle>

