Originally I was thinking to hook up a function with the CurrentCellChanged event, and then when the cell is changed, I manually to find the columns that need to be highlighted, and apply a style to it. but it is ugly. so I am keep searching whether there is a better way to do that, finally I get it.
In terms of select the column header, First of all, we should create a style and bind it to the column header, put the following xaml expression to your style, trigger part.
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource IsHeaderCurrentConverter}">
<Binding RelativeSource="{RelativeSource Self}" />
<Binding Path="CurrentColumn" RelativeSource="{RelativeSource FindAncestor,AncestorType={x:Type DataGrid}}" />
</MultiBinding>
</DataTrigger.Binding>
<Setter TargetName="headerBorder" Property="Background"
Value="Red" />
</DataTrigger>
After that, create a convertor.
public class IsHeaderCurrentConverter : IMultiValueConverter
{
#region IMultiValueConverter Members
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool bResult = false;
if (values[1] != null)
{
DataGridColumnHeader dgch = values[0] as DataGridColumnHeader;
System.Windows.Controls.DataGridTextColumn dgtc =
values[1] as System.Windows.Controls.DataGridTextColumn;
if(dgtc.Header.ToString().CompareTo(dgch.Column.Header.ToString()) == 0)
{
bResult = true;
}
}
return bResult;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
In the converter, if we return true, then the column header will be highlight with red.
Use the same theory , you can create a style to highlight the cells that you need to change when cell is selected.
No comments:
Post a Comment