I fill an ObservableCollection<>
(also tried a List<>
) with a custom Class. I've bound the collection to the DataGrid and defined the columns. Now I want to select a cell and have the other cells (in the same column although data in other columns won't ever match) that have the same string in it to be highlighted.
<Window.Resources>
<local:CellHighlighterConverter x:Key="cellHighlighterConverter" />
<CollectionViewSource x:Key="ScanCollectionViewSource" CollectionViewType="ListCollectionView" />
<Style x:Key="CenterCell" TargetType="TextBlock">
<Setter Property="TextBlock.TextAlignment" Value="Center" />
</Style>
<Style x:Key="CellPad" TargetType="TextBlock">
<Setter Property="Margin" Value="15,0,15,0" />
</Style>
<Style x:Key="CellHighlighterStyle" TargetType="TextBlock" >
<Setter Property="Background" Value="{Binding IsMatching, NotifyOnSourceUpdated=True, Converter={StaticResource cellHighlighterConverter}}" />
<Setter Property="TextBlock.TextAlignment" Value="Center" />
</Style>
</Window.Resources>
<Grid HorizontalAlignment="Center" >
<DataGrid x:Name="scans" DataContext="{StaticResource ScanCollectionViewSource}" ItemsSource="{Binding}" AutoGenerateColumns="False" FontFamily="Lucida Console" Margin="10" MouseUp="scans_MouseUp" >
<DataGrid.Columns>
<DataGridTextColumn Header="Device Name" Binding="{Binding Hostname}" Width="125" />
<DataGridTextColumn Header="Scan Date" Binding="{Binding ScanDate}" Width="75" ElementStyle="{StaticResource CenterCell}" />
<DataGridTextColumn Header="GUID" Binding="{Binding GUID}" Width="300" ElementStyle="{StaticResource CenterCell}" />
<DataGridTextColumn Header="MAC" Binding="{Binding MAC}" Width="105" ElementStyle="{StaticResource CellHighlighterStyle}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
Code behind
private ObservableCollection<Scan> ReadFiles()
{
scanList = new List<Scan>();
...
foreach(string file in files)
{
Scan newScan = new Scan();
...fill newScan with data
scanList.Add(newScan);
}
scanList = scanList.OrderBy(x => x.Hostname).ThenByDescending(x => x.ScanDate).ToList();
scanCollection = new ObservableCollection<Scan>(scanList);
return scanCollection;
}
Ways I've tried to accomplish this. Added a property to the class, then on mouse up, set that property if data matches (this works), then use a converter to set the background based on that data. Either the grid isn't refreshing or something else is wrong. Am I on the right track or is there a better way?
private void scans_MouseUp(object sender, MouseButtonEventArgs e)
{
string selMAC = ((Scan)((DataGrid)sender).SelectedValue).MAC;
foreach (Scan scan in scanList)//((DataGrid)sender).Items.OfType<Scan>().ToList() )
{
// compare values
if (scan.MAC == selMAC)
{
scan.IsMatch = true;
} else
{
scan.IsMatch = false;
}
//scans.Items.Refresh();
}
}
}
public class CellHighlighterConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if( (Boolean)value )
{
return new SolidColorBrush(Colors.Green);
}
return SystemColors.AppWorkspaceColor;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class Scan
{
public Scan()
{
this.IsMatch = false;
}
public string Hostname { get; set; }
public string ScanDate { get; set; }
public string GUID { get; set; }
public string MAC { get; set; }
public bool IsMatch { get; set; }
}
Scan
should implement INotifyPropertyChanged
interface and raise notificattion for IsMatch
property:
private bool _IsMatch;
public bool IsMatch
{
get { return _IsMatch; }
set
{
if (_IsMatch == value) return;
_IsMatch = value;
OnPropertyChanged("IsMatch");
}
}
without such notification Convert method isn't triggered even if you change IsMatch.
other things I would fix:
Convert
method should return Brush, not Color
{
return (bool)value ? Brushes.Green : SystemColors.AppWorkspaceBrush;
}
use SelectionChanged
event instead of MouseUp
. MouseUp can happen on the same cell multiple times, no need to search mathes each time
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.