c#wpf DataGridCell从convertor设置值

时间:2017-05-21 17:59:48

标签: c# wpf datagrid wpf-controls wpfdatagrid

我有这个DataGridCell,它调用这个转换器。我期待这个细胞的价值是"嗨" (当我通过转换器在数据网格中显示时,我将单元格"内容"设置为" hi")。

我在这里做错了什么?

<DataGridTextColumn Width="60" Header="Google" CanUserResize="True" CanUserSort="True">
    <DataGridTextColumn.HeaderStyle>
        <Style TargetType="{x:Type DataGridColumnHeader}">
            <Setter Property="ToolTip" Value="Current Position on Google" />
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>
    </DataGridTextColumn.HeaderStyle>
    <DataGridTextColumn.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <EventSetter Event="MouseUp" Handler="IdUnselect"/>

            <Setter Property="Background"   Value="{Binding GoogleKeywordPositionMovementSinceLastWeekCheck, Converter={StaticResource NameToBrushConverter}}"/>
            <Setter Property="Content"      Value="{Binding Path=., Converter={StaticResource GooglePositionConvertor}}"/>

        </Style>
    </DataGridTextColumn.CellStyle>

</DataGridTextColumn>



public class GooglePositionConvertor : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        ResultCheckObject RankCheck = value as ResultCheckObject;

        return "hi";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

1 个答案:

答案 0 :(得分:0)

通过以下方式通过转换器设置内容无效。

<Setter Property="Content" Value="{Binding Path=., Converter={StaticResource GooglePositionConvertor}}"/>

通过以下转换器进行设置

<DataGridTextColumn Width="60" Header="Google" Binding="{Binding Path=., Converter={StaticResource GooglePositionConvertor}}" CanUserResize="True" CanUserSort="True">