C# 转换器获取DataRowView而不是DataGridCell

C# 转换器获取DataRowView而不是DataGridCell,c#,wpf,datagrid,dataview,converters,C#,Wpf,Datagrid,Dataview,Converters,DataGrid.ItemSource在代码隐藏中设置为dataview。列名和计数需要动态更改,因此对象列表无法正常工作 我的桌面显示器工作完全正常。这很好,但是我的GridCellStyle将DataRowView传递给转换器,而不是我期望传递的DataGridCell 有没有办法获取DataGridCell内容或指示当前传递给转换器的列 <UserControl x:Class="TestUI.SilveusTable" xmlns="http://sche

DataGrid.ItemSource
在代码隐藏中设置为
dataview
。列名和计数需要动态更改,因此对象列表无法正常工作


我的桌面显示器工作完全正常。这很好,但是我的GridCellStyle将DataRowView传递给转换器,而不是我期望传递的DataGridCell

有没有办法获取DataGridCell内容或指示当前传递给转换器的列

<UserControl x:Class="TestUI.SilveusTable"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:TestUI"
             mc:Ignorable="d" 
             >
    <UserControl.Resources>
        <local:CbotValueConverter x:Key="CbotValueConverter" />
        <Style x:Key="GridCellStyle" TargetType="{x:Type DataGridCell}">
            <Setter Property="Background" Value="Yellow"/>
            <Setter Property="Foreground" Value="{Binding Converter={StaticResource CbotValueConverter}}"/>
        </Style>
    </UserControl.Resources>
    <Grid>
        <DataGrid x:Name="DataGrid1" IsReadOnly="True" CellStyle="{StaticResource GridCellStyle}" />
    </Grid>
</UserControl>

更改绑定的源

使用currect绑定声明,DataGridCell的DataContext是源

Value="{Binding Converter={StaticResource CbotValueConverter}}"
要使用DataGridCell本身,请添加
RelativeSourceSelf
part(指要在其上设置绑定的元素)


“这是我期望通过的。”这是一个错误的期望。除非显式设置绑定源(按源、RelativeSource或ElementName),否则绑定将使用当前DataContext作为其源。这里是Items或ItemsSource集合中的当前元素。考虑到样式绑定到DataGridCell,并且标记是CellStyle,期望它将绑定到的东西传递到转换器似乎是一个非常合理的期望。您还将如何基于单元格的内容执行转换?样式不绑定到任何内容。样式设置器中的属性可以绑定到当前DataContext的属性,除非绑定显式指定不同的源。除此之外,单元格的内容不是DataGridCell。请参见此处:.@RichardJune,尝试RelativeSource Self绑定一个单元格
{Binding Converter={StaticResource CbotValueConverter},RelativeSource={RelativeSource Self}
@ASh,这回答了我的问题。如果你把你的评论写在回答中,我会把它标为完整。
Value="{Binding Converter={StaticResource CbotValueConverter}}"
Value="{Binding Converter={StaticResource CbotValueConverter}, 
                RelativeSource={RelativeSource Self}}"