[英]C# wpf Datagrid with AutoGenerateColumns and Cellbackground
I'm have a WPF-app with a DataGrid that connects to different .ItemsSource, builds columns, works fine.我有一个带有 DataGrid 的 WPF 应用程序,它连接到不同的 .ItemsSource,构建列,工作正常。
Most of the time the first column has an ID of some sort to query the backend data.大多数情况下,第一列具有某种类型的 ID 来查询后端数据。 I want to colorize some cells of each row with different colors depending on that backend data.
我想根据后端数据为每一行的某些单元格着色不同的颜色。
Problem: my IValueConverter does not deliver the row I'm in. So within a cell coloring event I get the content of that cell but I found no way to access the ID in the first col of that row...问题:我的 IValueConverter 没有提供我所在的行。所以在单元格着色事件中,我获得了该单元格的内容,但我发现无法访问该行第一列中的 ID...
Anything pushing me into the right direction would be appreciated.任何将我推向正确方向的事情都将不胜感激。
I prepared some working demo code, can be copy/pasted into a new wpf-app:我准备了一些工作演示代码,可以复制/粘贴到一个新的 wpf-app 中:
XAML XAML
<Window x:Class="Datagridtest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Datagridtest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DataGrid x:Name="dgrid" HorizontalAlignment="Left" Height="255" Margin="98,74,0,0" VerticalAlignment="Top" Width="615"/>
</Grid>
</Window>
.cs Code .cs 代码
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
namespace Datagridtest
{
public class Data
{
public string id { get; set; }
public string name { get; set; }
public string location { get; set; }
}
public class DataConti
{
public List<Data> daten = new List<Data>();
public void MakeDummy()
{
var rand = new Random();
for (int i = 0; i < 20; i++)
{
Data d = new Data
{
name = "egal" + i.ToString(),
location = "egal" + i.ToString()
};
d.id = rand.Next(1000).ToString();
daten.Add(d);
}
}
}
class ValueConverterConti
{
public DataConti conti { get; set; }
public DataGridColumn col { get; set; }
}
class VC : IValueConverter // IMultiValueConverter?
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
ValueConverterConti c = (ValueConverterConti)parameter;
SolidColorBrush sb = Brushes.White;
if (c.col.DisplayIndex == 1)
{
// all with id%3==0--> Blue
sb = Brushes.LightBlue;
}
else
if (c.col.DisplayIndex == 2)
{
// all with id%2==0--> Yellow
sb = Brushes.Yellow;
}
return sb;
}
public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public partial class MainWindow : Window
{
DataConti dataconti = new DataConti();
void DataGridAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
VC vc = new VC()
{
// ?
};
ValueConverterConti dc = new ValueConverterConti
{
col = e.Column,
conti = dataconti
};
Style style = new Style(typeof(DataGridCell));
style.Setters.Add(new Setter(DataGridCell.BackgroundProperty,
new Binding(e.PropertyName)
{
Converter = vc,
ConverterParameter = dc
}));
e.Column.CellStyle = style;
}
public MainWindow()
{
InitializeComponent();
dataconti.MakeDummy();
dgrid.AutoGenerateColumns = true;
dgrid.AutoGeneratingColumn += DataGridAutoGeneratingColumn;
dgrid.ItemsSource = dataconti.daten;
}
}
}
So the solution to this problem is using MultiBinding.所以这个问题的解决方案是使用MultiBinding。
public class QuantityToBackgroundConverterM : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
SolidColorBrush sb = Brushes.White;
try
{
if (values[0] != DependencyProperty.UnsetValue)
{
int i = System.Convert.ToInt32(values[0]);
string column = (string)values[1];
switch (column)
{
case "name":
if (i % 2 == 0) { sb = Brushes.LightBlue; }
break;
case "location":
if (i % 3 == 0) { sb = Brushes.Yellow; }
break;
}
}
}
catch (Exception){}
return sb;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public partial class MainWindow : Window
{
DataConti dataconti = new DataConti();
void DataGridAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
Binding bid = new Binding();
bid.Path = new PropertyPath("id");
Binding bheadername = new Binding();
bheadername.Source = e.Column.Header as String;
Binding bcolumn = new Binding();
bcolumn.Path = new PropertyPath(e.Column.Header as String);
MultiBinding binder = new MultiBinding();
binder.Bindings.Add(bid);
binder.Bindings.Add(bheadername);
binder.Bindings.Add(bcolumn);
binder.Converter = new QuantityToBackgroundConverterM();
Setter setter = new Setter(DataGridRow.BackgroundProperty, binder);
Style style = new Style(typeof(DataGridCell));
style.Setters.Add(setter);
e.Column.CellStyle = style;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.