XAML 如何按值更改DataGrid的Border的BorderBrush

1rhkuytd  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(111)

你好,我正在尝试根据记录的值更改DataGrid中的Border元素的BorderBrush属性。例如,如果记录的值为“低”,BorderBrush的颜色将更改为绿色,值为“中”,颜色将为黄色等。我如何才能实现这一点?我制作了CellConverter,但如何将它用于Border?

<Style x:Key="DataGridRowStyle2" TargetType="{x:Type DataGridRow}">
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="SnapsToDevicePixels" Value="true"/>
                <Setter Property="BorderBrush" Value="Transparent"/>
                <Setter Property="ValidationErrorTemplate">
                    <Setter.Value>
                        <ControlTemplate>
                            <TextBlock Foreground="Transparent" Margin="2,0,0,0" Text="!" VerticalAlignment="Center"/>
                            
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridRow}">
                          //This one
                            <Border x:Name="DGR_Border"  CornerRadius="5" Background="{TemplateBinding Background}" BorderThickness="2" BorderBrush="Red" SnapsToDevicePixels="True">
                                <SelectiveScrollingGrid>
                                    
                                    <SelectiveScrollingGrid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                    </SelectiveScrollingGrid.ColumnDefinitions>
                                    <SelectiveScrollingGrid.RowDefinitions>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition Height="Auto"/>
                                    </SelectiveScrollingGrid.RowDefinitions>
                                    <DataGridCellsPresenter ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                    <DataGridDetailsPresenter Visibility="{TemplateBinding DetailsVisibility}"/>
                                    <DataGridRowHeader Visibility="{Binding HeadersVisibility, Converter={x:Static DataGrid.HeadersVisibilityConverter}, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                </SelectiveScrollingGrid>
                                
                            </Border>
                            
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>

CellConvertor类别

public class CellConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string input = value as string;
            switch (input)
            {
                case "High":
                return Brushes.Red;
                   
                case "Medium":
                    return Brushes.Yellow;
                    
                case "Low":
                    return Brushes.Green;
                default:
                    return DependencyProperty.UnsetValue;
            }
        }

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

b1uwtaje1#

您可以通过多种方式实现此行为。
1.您可以让色彩属性(什至string)根据传回“高”、“低”、“中”值的属性来传回。

视图模型:

private string _Colour { get; set; }
private string _Value { get; set; }

public string Colour
{
    get { return _Colour; }
}

public string Value 
{
    get { return _Value; }
    set
    {
        _Value = value;
        switch (_Value)
        {
            case "Low":
                Colour = "Green";
                break;
            case "Medium":
                Colour = "Yellow";
                break;
            case "High":
                Colour = "Red";
                break;
            default:
                Colour = null;
                break;
            
        }
        OnPropertyChanged("Colour");
        OnPropertyChanged("Value");
    }
}

XAML文件:

<!--And other properties-->
<Border Background="{Binding Colour}"/>

1.另一种方法是通过转换器,尽管我仍然会将其作为字符串返回

internal class StringColourConverter : IValueConverter
 {
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
     {
         if (value is not string) return null;
         switch (value)
         {
             case "Low":
                 return  "Green";
             case "Medium":
                 return  "Yellow";
             case "High":
                 return  "Red";
             default:
                 return  null;
         }
     }

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

}
但在本例中绑定是不同的,您必须引用此转换器;

xmlns:converter="clr-namespace:AssemblyName.xamlconverters"
<DataGrid>
    <DataGrid.Resources>
        <ResourceDictionary>
            <converter:StringColourConverter x:Key="StrCol"/>
        </ResourceDictionary>
    </DataGrid.Resources>
    
    <!--And other properties-->
    <Border Background="{Binding Value, Converter={StaticResource StrCol}}"/>

此外,使用Converter时,您不需要ViewModel上的Color属性

相关问题