wpf XamDataGrid CellValuPresenter 工具提示样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12467012/
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
XamDataGrid CellValuPresenter tooltip style
提问by Flack
I am trying to create a style for my XamDataGrid so that I can determine what values to show in a tooltip based on the current cell that is being hovered over.
我正在尝试为我的 XamDataGrid 创建一个样式,以便我可以根据悬停在上面的当前单元格确定在工具提示中显示哪些值。
I am trying the below:
我正在尝试以下内容:
<Style x:Key="MyCVPStyle" TargetType="{x:Type igDP:CellValuePresenter}">
<Setter Property="ToolTip">
<Setter.Value>
<StackPanel>
<ListView ItemsSource="{Binding RelativeSource={...}, Path=Field, Converter={StaticResource MyFieldConverter}}">
...
The DataContext of the style is set to the DataRecord. The problem I am having is that I do not know how to access the actual field of the cell value presenter.
样式的 DataContext 设置为 DataRecord。我遇到的问题是我不知道如何访问单元格值演示者的实际字段。
I tried setting the source to:
我尝试将源设置为:
{Binding RelativeSource={RelativeSource AncestorType={x:Type igDP:CellValuePresenter}}, Path=Field
but that fails with a binding error:
但由于绑定错误而失败:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='Infragistics.Windows.DataPresenter.CellValuePresenter', AncestorLevel='1''. BindingExpression:Path=Field; DataItem=null; target element is 'ListView' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')
How can I access/bind to the CellValuePresenter.Field so I can pass that value to my converter?
我如何访问/绑定到 CellValuePresenter.Field 以便我可以将该值传递给我的转换器?
As a reference, I have other CellValuePresenter styles that work without issue where I access the Field. For example, here the second binding param is the Field, accessed by referencing Self:
作为参考,我有其他 CellValuePresenter 样式可以在我访问 Field 时正常工作。例如,这里的第二个绑定参数是 Field,通过引用 Self 来访问:
<Setter Property="BorderThickness">
<Setter.Value>
<MultiBinding Converter="{StaticResource BorderThicknessConverter}">
<MultiBinding.Bindings>
<Binding Path="DataItem" />
<Binding RelativeSource="{RelativeSource Self}" Path="Field" />
</MultiBinding.Bindings>
</MultiBinding>
</Setter.Value>
</Setter>
回答by Matt McCann
You could use the PlacementTarget property of ToolTip, which will be the CellValuePresenter, and set it as the DataContext that the ListView is binding to:
您可以使用 ToolTip 的 PlacementTarget 属性,即 CellValuePresenter,并将其设置为 ListView 绑定到的 DataContext:
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip DataContext="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget}">
<StackPanel>
<ListView ItemsSource="{Binding Path=Field, Converter={StaticResource MyFieldConverter}}"></ListView>
</StackPanel>
</ToolTip>
</Setter.Value>
</Setter>
回答by FodderZone
I found an alternate solution that I feel is more straight forward.
我找到了一个我觉得更直接的替代解决方案。
Create the following style (I believe the property you are trying to bind ItemsSource to is named Field)
创建以下样式(我相信您尝试将 ItemsSource 绑定到的属性名为 Field)
<Style x:Key="MyCVPStyle" TargetType="{x:Type igDP:CellValuePresenter}">
<Setter Property="ToolTip">
<Setter.Value>
<StackPanel>
<ListView ItemsSource="{Binding Path=Cells[Field].Value}" />
</StackPanel>
</Setter.Value>
</Setter>
</Style>
But this does require that you have the field you're binding your ItemsSource to defined in FieldLayout.Fields. You may want to set the Visibility of that field to collapsed. I've also included the field that has the tooltip style applied to it.
但这确实要求您拥有将 ItemsSource 绑定到 FieldLayout.Fields 中定义的字段。您可能希望将该字段的可见性设置为折叠。我还包括了应用了工具提示样式的字段。
<igDP:Field Label="Value" Name="Value" >
<igDP:Field.Settings>
<igDP:FieldSettings CellValuePresenterStyle="{StaticResource ResourceKey=MyCVPStyle}" />
</igDP:Field.Settings>
</igDP:Field>
<igDP:Field Label="Field" Name="Field" Visibility="Collapsed" />
回答by alhalama
You could use a different style for the different fields within the XamDataGrid so that each style would already be set up for the specific field.
您可以对 XamDataGrid 中的不同字段使用不同的样式,以便已经为特定字段设置了每种样式。
The reason why the binding is failing is because the tooltip is shown in a pop up so it is another window and not in the same visual tree as the CellValuePresenter.
绑定失败的原因是工具提示显示在弹出窗口中,因此它是另一个窗口,而不是与 CellValuePresenter 位于同一可视树中。

