我尝试在DevExpress网格控件中将Tooltip绑定到单元格值,但是 我在Setter.Value属性“丢失”了DataContext:
这里是GridControl的完整代码(只有一列):
<dxg:GridControl Grid.Row="0"
x:Name="grid"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
dx:ThemeManager.ThemeName="Seven"
ScrollViewer.CanContentScroll="True"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding ObjectViewModel.Collection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding CurrentElement,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,TargetNullValue=null}"
>
<dxg:GridControl.View>
<!--region #RowCellMenuCustomization-->
<dxg:TableView x:Name="view" AutoWidth="True"
UseLightweightTemplates="None"
>
</dxg:TableView.RowCellMenuCustomizations>
</dxg:TableView>
<!--endregion #RowCellMenuCustomization-->
</dxg:GridControl.View>
<dxg:GridControl.Columns>
<dxg:GridColumn
Header="Address"
Binding="{Binding Address,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"
AllowEditing="False"
HorizontalHeaderContentAlignment="Stretch"
Width="*"
AllowResizing="True"
HeaderToolTip="Address"
>
<dxg:GridColumn.CellStyle
>
<Style x:Name="toolTipStyle"
BasedOn="{StaticResource {dxgt:GridRowThemeKey ResourceKey=CellStyle}}"
TargetType="dxg:GridCellContentPresenter">
<Setter Property="ToolTip">
<Setter.Value>
<TextBlock Text="{Binding Path=Address,RelativeSource={RelativeSource Self},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
</Setter.Value>
</Setter>
</Style>
</dxg:GridColumn.CellStyle>
</dxg:GridColumn>
</dxg:GridControl.Columns>
</dxg:GridControl>
集合类代码:
public class Element
{
public String Address
{
return SomeObject.Address;
}
// other properties
}
所以,感谢this answer:它适用于一些不绑定的文本,但是当我尝试将它绑定到属性时,它不起作用。
Visual Studio输出窗口日志:
System.Windows.Data Error: 40 : BindingExpression path error: 'Address' property not found on 'object' ''TextBlock' (Name='')'. BindingExpression:Path=Address; DataItem='TextBlock' (Name=''); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
看来,我丢失了DataContext,但是如何在Setter.Value上绑定它?
P.S。 @ Rekshino,我这样做,输出日志是:
System.Windows.Data Error: 40 : BindingExpression path error: 'Address' property not found on 'object' ''EditGridCellData' (HashCode=41748728)'. BindingExpression:Path=DataContext.Address; DataItem='TextBlock' (Name=''); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
答案 0 :(得分:2)
试试这个:
<Setter.Value>
<TextBlock Text="{Binding RowData.Row.Address}"/>
</Setter.Value>
或者:
<dxg:GridColumn.CellStyle>
<Style x:Name="toolTipStyle"
BasedOn="{StaticResource {dxgt:GridRowThemeKey ResourceKey=CellStyle}}"
TargetType="dxg:GridCellContentPresenter">
<Setter Property="ToolTip" Value="{Binding RowData.Row.Address}"/>
</Style>
</dxg:GridColumn.CellStyle>
答案 1 :(得分:0)
尝试
<TextBlock Text="{Binding Path=DataContext.Address,RelativeSource={RelativeSource Self},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
您尝试绑定到TextBlock而不是它的DataContext