이
는) 지금까지 (최적하지만 작동하는 것 같다 만약 내가 내 문제, (확실하지를 해결하는 방법입니다
<Style TargetType="{x:Type DataGridCell}">
<Style.Setters>
<Setter Property="Background">
<Setter.Value>
<MultiBinding Converter="{StaticResource onErrorConverter}">
<Binding RelativeSource="{RelativeSource Self}" />
<Binding RelativeSource="{RelativeSource AncestorType=SampleTests:SampleTestUserControlBase}" Path="DataContext.Problems" />
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}" />
</MultiBinding>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
그리고 컨버터 :
public class DataGridCellOnErrorConversion : IMultiValueConverter
{
private readonly SolidColorBrush DefaultColour = new SolidColorBrush(Colors.White);
private readonly SolidColorBrush ErrorColour = new SolidColorBrush(Colors.Red);
private readonly SolidColorBrush AlternatingColour = new SolidColorBrush(Colors.AliceBlue);
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length < 3)
return DefaultColour;
DataGridCell dgc = values[0] as DataGridCell;
if(dgc == null)
return DefaultColour;
IList<Point> problems = values[1] as IList<Point>;
if(problems == null)
return DefaultColour;
DataGrid grid = values[2] as DataGrid;
if (grid == null)
return DefaultColour;
int x;
int y = -1;
ItemCollection itemCollection = grid.Items;
for (int i = 0; i < itemCollection.Count; i++)
{
if (itemCollection.CurrentItem == itemCollection[i])
y = i;
}
x = dgc.Column.DisplayIndex;
DataRowView currentRowView = null;
FieldInfo fi = dgc.GetType().GetField("_owner", BindingFlags.NonPublic | BindingFlags.Instance);
try
{
if (fi != null)
{
DataGridRow dataGridRow = fi.GetValue(dgc) as DataGridRow;
if(dataGridRow != null)
currentRowView = dataGridRow.Item as DataRowView;
}
}
catch (InvalidCastException) { }
if(currentRowView != null)
{
for (int i = 0; i < itemCollection.Count; i++)
{
if (currentRowView == itemCollection[i])
y = i;
}
}
if (problems.Any(problem => System.Convert.ToInt32(problem.X) == x && System.Convert.ToInt32(problem.Y) == y))
{
return ErrorColour;
}
return y % 2 == 0 ? AlternatingColour : DefaultColour;
}
}
출처
2011-04-26 14:49:08
Jon
이 (목록에)이 _'point'_ (즉,이 데이터 집합의 각 행은 열 이름을 가질 수 있습니다 'IsBad'로 입력 된 데이터 세트의 일부가 될 수 있습니다 부울 va lue)? 이 경우에는 변환기를 사용하여 백그라운드를 부울로 바인딩 할 수 있습니다. –
publicgk
@publicgk 안타깝게도 전체 셀을 표시하는 것은 옵션이 아닙니다. – Jon
그래서 포인트의 X, Y는 DataGrid의 x, y 셀 번호를 나타 냅니까? – publicgk