質問

動的に生成された列を持つDataGridがあり、いくつかの操作の後に値が変更されたセルのスタイルを変更する必要があります。

DataGridのItemsSourceは次のように定義されています List<MyTableRow>, 、ここで

public class MyTableRow
{
    private string[] _values;
    private string _rowHeader;

    // getters and setters here
}

DataGrid列は、次のコードで生成されます:

for (int i = 0; i < source[0].Values.Length; i++)
{
    var col = new DataGridTextColumn();
    var binding = new Binding("Values[" + i + "]");
    col.Binding = binding;
    col.CanUserSort = false;
    this.dataGrid.Columns.Add(col);
    this.dataGrid.Columns[i].Header = columnNames[i];
}

結果のDataGridは次のようになります これは...

ItemsSourceの値が変更されたセル(太字のテキストまたは色付きの背景)を強調表示しようとすると、問題が発生します。これは私の質問が2つに分かれているポイントです:

  1. 変更されたセルでsmthを行うための「組み込み」方法はありますか?(たぶん ObservableColletion または他のsmth)
  2. いいえの場合、インデックスまたは値に基づいて別々のセルを強調表示するにはどうすればよいですか

Xamlスタイルやトリガーでこれを実行しようとしましたが、どの値をコンバーターに渡すべきかわからないことがわかりました

<Style TargetType="TextBlock">
    <Setter Property="Background" 
            Value="{Binding <!-- some proper binding here -->, 
                    Converter={StaticResource ValueToBrushConverter}}"/>
</Style>

SOで見つかった他の解決策は、バインディングと同じ「問題」を持っているか、単に機能しません。私はちょうど強調表示するために何ができますか ワン セルと行/列全体ではありませんか?アイテムを変更できますソース, MyTableRow 必要に応じて、フィールドおよび/または列の生成コード

誰でも私を助けてくれますか?私はこの問題で立ち往生してから数日でした


更新 見つかったソリューション


役に立ちましたか?

解決

最後に、私がやりたいことをする方法を見つけてください。解決策はちょっと「汚い」ですが、私にとってはうまくいきます。強調表示する必要があるすべてのセルに改行しないスペース文字を追加しました

private const string NBSP = "\u00A0"

その後に残っているのは、value converterを作成することだけです。だから私は追加しました MultiBinding 私のXAMLで:

<DataGrid.CellStyle>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="Background">
            <Setter.Value>
                <MultiBinding Converter="{StaticResource ValueToBrushMultiConverter}" >
                    <MultiBinding.Bindings>
                        <Binding RelativeSource="{RelativeSource Self}" />
                        <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}" />
                    </MultiBinding.Bindings>
                 </MultiBinding
            </Setter.Value>
        </Setter>
    </Style>
</DataGrid.CellStyle>

コンバータとして定義されています:

public class ValueToBrushMultiConverter : IMultiValueConverter
    {
        private const string NBSP = "\u00A0";
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var cell = (DataGridCell)values[0];
            var dgRow = (DataGridRow)values[1];

            var test = (dgRow.Item as TableRow<string, string>).Values[cell.Column.DisplayIndex];

            if (test.Contains(NBSP))
                return System.Windows.Media.Brushes.PaleGreen;
            return DependencyProperty.UnsetValue;           
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

これが誰かを助けることを願っています!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top