如何在 Setter 值 属性 中查找 DataContext?
How to find DataContext in Setter Value property?
我尝试将工具提示绑定到 DevExpress 网格控件中的单元格值,但是
我 "lost" DataContext 在 Setter.Value 属性:
这里是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>
Collection class 代码:
public class Element
{
public String Address
{
return SomeObject.Address;
}
// other properties
}
所以,感谢 :它适用于一些非绑定文本,但是当我尝试将它绑定到 属性 时它不起作用。
Visual Studio输出window日志:
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')
尝试
<TextBlock Text="{Binding Path=DataContext.Address,RelativeSource={RelativeSource Self},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
您已尝试绑定到 TextBlock 而不是它的 DataContext
试试这个:
<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>
我尝试将工具提示绑定到 DevExpress 网格控件中的单元格值,但是 我 "lost" DataContext 在 Setter.Value 属性:
这里是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>
Collection class 代码:
public class Element
{
public String Address
{
return SomeObject.Address;
}
// other properties
}
所以,感谢
Visual Studio输出window日志:
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')
尝试
<TextBlock Text="{Binding Path=DataContext.Address,RelativeSource={RelativeSource Self},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
您已尝试绑定到 TextBlock 而不是它的 DataContext
试试这个:
<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>