1

カスタムの方法でデータグリッドの 1 つの列をバインドする際に 1 つの問題があります。だから、私はこのコードを表示しています:

<DataGridTemplateColumn Header="State">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Label Background="" Content="{Binding Path=., Converter={StaticResource measureConv}}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

私のコンバーター:

public class MeasureToStateConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Measure m;
        try
        {
            m = (Measure)value;
            if (m.Value > 100)
            {
                return "Alarm";
            }
        }
        catch (Exception ex)
        {
            Debugger.Log(0, "Convertery", "Bład Convertera MeasureToState" + ex.Message);
        }
        return "Normal";
    }

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

コンテンツと背景のプロパティはカスタムです。コンバーターを使用して、コレクション オブジェクトが何らかの条件を満たしているかどうかを確認し、文字列 YES または NO を返しますが、ある色の背景で文字列フィールドを YES にしたい場合、それが NO の場合は別の色にします。

どうすれば簡単にできますか?2番目のコンバーターの書き込みは少しばかげていると思います。

4

3 に答える 3

1

DataTemplates2 つのそれぞれのラベルを使用して 2つを作成しDataTemplateSelector、値コンバーターの代わりに を使用して適切なテンプレートを取得できます。

于 2011-03-12T11:10:21.183 に答える
0

コンバーターを再利用して背景を設定し、DataTrigger を使用してコンテンツを変更できます。
あなたの Measure オブジェクトのフィールドは Value と呼ばれていると思います。

コンバーターを適用して、列の内容を DataGrid に表示します。

<DataGridTextColumn 
   Header="State" 
   Width="SizeToHeader"
   Binding="{Binding Value, Converter={StaticResource measureConv}}" 
   CellStyle="{StaticResource ResourceKey=BackgroundCellStyle}"
   FontSize="20" />

コンバーターを適用してスタイルを変更します。

<Window.Resources>
  <Style TargetType="{x:Type DataGridCell}" x:Key="BackgroundCellStyle">
    <Setter Property="Background" Value="Aqua"/>
    <Style.Triggers>
      <DataTrigger Binding="{Binding Path=Value, Converter={StaticResource measureConv}}" Value="Alarm">
        <Setter Property="Background" Value="Chartreuse"/>
      </DataTrigger>
    </Style.Triggers>
  </Style>
</Window.Resources>
于 2011-03-12T16:49:34.837 に答える
0

Label の Background プロパティを独自の Content プロパティにバインドし、コンバーターを使用して目的の Brush を返します。

<Label Background="{Binding Path=Content, RelativeSource={RelativeSource Self}, Converter={StaticResource ContentToBrushConverter}"/>

コンバーターは Content プロパティの値を受け取ります...「はい」の場合は Brushes.Green を返し、「いいえ」の場合は Brushes.Red を返します。

于 2011-03-12T15:27:17.373 に答える