你好,我正在尝试根据记录的值更改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();
}
}
1条答案
按热度按时间b1uwtaje1#
您可以通过多种方式实现此行为。
1.您可以让色彩属性(什至
string
)根据传回“高”、“低”、“中”值的属性来传回。视图模型:
XAML文件:
1.另一种方法是通过转换器,尽管我仍然会将其作为字符串返回
}
但在本例中绑定是不同的,您必须引用此转换器;
此外,使用Converter时,您不需要
ViewModel
上的Color
属性