如果来自WPF DataGrid的SelectionChanged事件中的条件为真,我想更改整行的颜色。如果我使用arg DataGridRowEventArgs e,我可以更改LoadingRow事件中的颜色。我可以使用e.Row.Background = new SolidColorBrush(Colors.White)。但是这在SelectionChangedEvent中不起作用。如何从DataGrid转换为DataGridRow以使用DataGridRow.Row.Background = new SolidColorBrush(Colors.White)? 谢谢你的理解。
private void grd_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int iIndex = grd.SelectedIndex;
SolidColorBrush myColor = new SolidColorBrush(Colors.Tomato);
if (grd.SelectedItem != null)
{
// var datagridrow = grd.SelectedItem as DataGridRow; --> dosent'work
var drw = grd.SelectedItem as DataRowView;
DataRow row = drw.Row;
if (Convert.ToBoolean(row["Checkbox"]) == true)
{
row["Checkbox"] = false;
}
else
{
row["Checkbox"] = true;
// change the background color of the row too what ever
// white red blue
}
}
}
=============================================== =================================
这里是xaml和代码。非常感谢你
MainWindow.xaml.cs
namespace DataGridBrushRowColor
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
CreateTable myDog = new CreateTable();
// Lade Daten
dataGrid1.AutoGenerateColumns = true;
DataView myView = new DataView(myDog.DogOutput);
dataGrid1.DataContext = myView;
}
private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var rowview = dataGrid1.SelectedItem as DataRowView;
if (rowview != null)
{
DataRow row = rowview.Row;
if (Convert.ToBoolean(rowview["Choice"]) == true)
{
row["Choice"] = false;
}
else
{
row["Choice"] = true;
// Color the whole row red
}
}
}
}
}
MainWindow.xaml
<Window x:Class="DataGridBrushRowColor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid AutoGenerateColumns="False" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top" Height="243" Width="503" ItemsSource="{Binding}" SelectionChanged="dataGrid1_SelectionChanged" >
</DataGrid>
<Button Content="Button" Height="26" HorizontalAlignment="Left" Margin="12,249,0,0" Name="button1" VerticalAlignment="Top" Width="100" Click="button1_Click" />
</Grid>
CreateTable.cs
namespace DataGridBrushRowColor
{
class CreateTable
{
DataTable dtDog;
public DataTable DogOutput
{
get
{
return dtDog;
}
}
public CreateTable()
{
// Here we create a DataTable with four columns.
DataTable table = new DataTable();
table.Columns.Add("Choice", typeof(Boolean));
table.Columns.Add("Weight", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Breed", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
// Here we add five DataRows.
table.Rows.Add(false,57, "Koko", "Shar Pei", DateTime.Now);
table.Rows.Add(false,130, "Fido", "Bullmastiff", DateTime.Now);
table.Rows.Add(false, 92, "Alex", "Anatolian Shepherd Dog", DateTime.Now);
table.Rows.Add(false, 25, "Charles", "Cavalier King Charles Spaniel", DateTime.Now);
table.Rows.Add(false, 7, "Candy", "Yorkshire Terrier", DateTime.Now);
dtDog = table;
}
}
}
答案 0 :(得分:0)
你可以通过使用样式和触发器来实现同样的目的。如下所示。
<Window.Resources>
<local:MyColorConverter x:Key="colorconverter" />
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{Binding Converter={StaticResource colorconverter}}" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
..代码背后的变化