Tob*_*sen 66 wpf xaml wpfdatagrid
在我的XAML代码中,我想Background根据一个特定行中对象的值设置每一行的颜色.我有一个ObservableCollection的z,并且每个z有一个叫做财产State.我开始用这样的东西在我的DataGrid:
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background"
Value="{Binding z.StateId, Converter={StaticResource StateIdToColorConverter}}"/>
</Style>
</DataGrid.RowStyle>
Run Code Online (Sandbox Code Playgroud)
这是一种错误的方法,因为x不是我的ViewModel类中的属性.
在我的ViewModel类我有一个ObservableCollection<z>是在ItemsSource这DataGrid,和SelectedItem类型z.
我可以绑定颜色SelectedItem,但这只会改变一行DataGrid.
我如何根据一个属性更改此行backgroundcolor?
Nit*_*esh 146
使用DataTrigger:
<DataGrid ItemsSource="{Binding YourItemsSource}">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding State}" Value="State1">
<Setter Property="Background" Value="Red"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding State}" Value="State2">
<Setter Property="Background" Value="Green"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
Vah*_*yan 15
同样可以没有DataTrigger:
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" >
<Setter.Value>
<Binding Path="State" Converter="{StaticResource BooleanToBrushConverter}">
<Binding.ConverterParameter>
<x:Array Type="SolidColorBrush">
<SolidColorBrush Color="{StaticResource RedColor}"/>
<SolidColorBrush Color="{StaticResource TransparentColor}"/>
</x:Array>
</Binding.ConverterParameter>
</Binding>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
Run Code Online (Sandbox Code Playgroud)
BooleanToBrushConverter以下课程在哪里:
public class BooleanToBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return Brushes.Transparent;
Brush[] brushes = parameter as Brush[];
if (brushes == null)
return Brushes.Transparent;
bool isTrue;
bool.TryParse(value.ToString(), out isTrue);
if (isTrue)
{
var brush = (SolidColorBrush)brushes[0];
return brush ?? Brushes.Transparent;
}
else
{
var brush = (SolidColorBrush)brushes[1];
return brush ?? Brushes.Transparent;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
在XAML中,为DataGrid添加并定义RowStyle属性,其目标是将Row的Background设置为在Employee对象中定义的Color。
<DataGrid AutoGenerateColumns="False" ItemsSource="EmployeeList">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="{Binding ColorSet}"/>
</Style>
</DataGrid.RowStyle>
Run Code Online (Sandbox Code Playgroud)
在我的员工班上
public class Employee {
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string ColorSet { get; set; }
public Employee() { }
public Employee(int id, string name, int age)
{
Id = id;
Name = name;
Age = age;
if (Age > 50)
{
ColorSet = "Green";
}
else if (Age > 100)
{
ColorSet = "Red";
}
else
{
ColorSet = "White";
}
}
}
Run Code Online (Sandbox Code Playgroud)
这样,每一个DataGrid的行具有背景色的的ColorSet 我的对象的属性。
| 归档时间: |
|
| 查看次数: |
89076 次 |
| 最近记录: |