【问题标题】:c# How do I pass the value of the DataGrid Cell into a converter?c# 如何将 DataGrid Cell 的值传递给转换器?
【发布时间】:2017-05-23 08:52:24
【问题描述】:

如何将DataGridCell 的值传递给Foreground 属性转换器?

所以GooglePositionConvertor 将返回一个由 Path= 传递的对象创建的值。但是我想根据GooglePositionConvertor 返回的值更改单元格样式的前景色。

<DataGridTextColumn Binding="{Binding Path=., Converter={StaticResource GooglePositionConvertor}}">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="Foreground" Value="{????????????, Converter={StaticResource ChangeBrushColour}}"/>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

【问题讨论】:

    标签: c# wpf wpfdatagrid


    【解决方案1】:

    只是不指定绑定路径 - Foreground 属性将接收 DataGridCell 的 DataContext 作为绑定源。

    <DataGrid ItemsSource="{Binding ColorList}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding}">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Setter Property="Foreground" Value="{Binding Converter={StaticResource ColorToBrush}}"/>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>
    

    【讨论】:

    • 我需要绑定到第一个转换器“GooglePositionConvertor”的行的对象,但只需要第二个转换器“ChangeBrushColour”的单元格值。
    【解决方案2】:

    您可以绑定到DataGridCellContent 属性并使用DependencyPropertyDescriptor 来设置TextBlockForeground 属性,一旦其Text 属性已设置:

    <DataGridTextColumn Binding="{Binding Path=., Converter={StaticResource GooglePositionConvertor}}">
        <DataGridTextColumn.CellStyle>
            <Style TargetType="{x:Type DataGridCell}">
                <Setter Property="Foreground" Value="{Binding Path=Content, RelativeSource={RelativeSource Self}, 
                    Converter={StaticResource ChangeBrushColour}}"/>
            </Style>
        </DataGridTextColumn.CellStyle>
    </DataGridTextColumn>
    

    public class Converter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            TextBlock content = value as TextBlock;
            if(content != null)
            {
                string text = content.Text;
                if(string.IsNullOrEmpty(text))
                {
                    DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(TextBlock.TextProperty, typeof(TextBlock));
                    if (dpd != null)
                        dpd.AddValueChanged(content, OnTextChanged);
                }
                else
                {
                    OnTextChanged(content, EventArgs.Empty);
                }
            }
            return Binding.DoNothing;
        }
    
        private void OnTextChanged(object sender, EventArgs e)
        {
            TextBlock textBlock = sender as TextBlock;
            string converterText = textBlock.Text;
            //set foreground based on text...
            textBlock.Foreground = Brushes.Violet;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-07
      • 2018-01-09
      • 2018-03-23
      • 1970-01-01
      • 2011-11-19
      • 2012-08-21
      • 2013-08-21
      • 1970-01-01
      相关资源
      最近更新 更多