开发者

Datagrid binding to collection, problem with one column converting

开发者 https://www.devze.com 2023-02-16 04:42 出处:网络
i have one problem with binding one column of my datagrid in custom way. So, i have this code in view:

i have one problem with binding one column of my datagrid in custom way. So, i have this code in view:

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

My Converter:

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();
    }
}

Content and Background Properties are custom. I use converter to check if my collection object fulfill some condition end return String YES, or NO, but if i want to have string field YES in one color background and if it is NO i another color.

How can i do it easy? I feel that write second converter is little stupid.


You could create two DataTemplates with the two respective labels and use a DataTemplateSelector instead of a value converter to get the right template.


Bind the Background property of the Label to it's own Content property and use a converter to return the desired Brush:

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

The converter will receive the value of the Content property... If it equals "Yes" return Brushes.Green, if it equals "No" return Brushes.Red


You can reuse the converter to set the background and alter the content using a DataTrigger.
I've assummed the field in your Measure object is called Value.

Apply the converter to display the column content in the DataGrid:

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

Apply the converter to alter the style:

<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>
0

精彩评论

暂无评论...
验证码 换一张
取 消