У меня есть DataGrid и несколько флажков, которые действуют как фильтры для видимости определенных строк в DataGrid. Это работает отлично, пока DataGrid не прокручивается; если вы попытаетесь отфильтровать DataGrid, установив один из флажков, преобразователь, который обрабатывает логику фильтрации, выдаст исключение, так как параметр значений в преобразователе теперь равен нулю.
Я не понимаю, почему это сначала работает, а потом ломается. Ниже представлен xaml и пример конвертера.
ПРЕОБРАЗОВАТЕЛЬ
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
//var softwareModel = (SoftwareModel)values[0];
//var checkBox = (CheckBox)values[1];
SoftwareModel softwareModel = values[0] as SoftwareModel;
CheckBox checkBox = values[1] as CheckBox;
if (checkBox.IsChecked == true)
{
foreach (var missingSoftwareItem in SharedClass.requiredSoftwareMissingOC) // for each missing software
{
if (SharedClass.SoftwareModelComparitor(softwareModel, missingSoftwareItem)) // if missing == total
{
return Visibility.Collapsed;
}
}
}
return Visibility.Visible;
}
XAML
<UserControl x:Class="NIThirdPartySoftwareChecker.Views.SoftwareChecksView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:NIThirdPartySoftwareChecker.ViewModels"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<local:SoftwareListContainsToDataGridRowStyleConverter x:Key="SoftwareListContainsToDataGridRowStyleConverter"/>
<local:RequiredInstalledSoftwareToVisibiltyConverter x:Key="RequiredInstalledSoftwareToVisibiltyConverter"/>
<local:RequiredMissingSoftwareToVisibiltyConverter x:Key="RequiredMissingSoftwareToVisibiltyConverter"/>
<local:UnrequiredSoftwareToVisibiltyConverter x:Key="UnrequiredSoftwareToVisibiltyConverter"/>
<Style x:Key="DataGridRowStyle"
TargetType="{x:Type DataGridRow}">
<Setter Property="Background"
Value="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource SoftwareListContainsToDataGridRowStyleConverter}}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=chbx_hideUnrequiredSoftware, Path=IsChecked}" Value="True">
<Setter Property="Visibility">
<Setter.Value>
<MultiBinding Converter="{StaticResource UnrequiredSoftwareToVisibiltyConverter}">
<Binding Path="."/>
<Binding ElementName="chbx_hideUnrequiredSoftware"/>
</MultiBinding>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=chbx_hideRequiredInstalledSoftware, Path=IsChecked}" Value="True">
<Setter Property="Visibility">
<Setter.Value>
<MultiBinding Converter="{StaticResource RequiredInstalledSoftwareToVisibiltyConverter}">
<Binding Path="."/>
<Binding ElementName="chbx_hideRequiredInstalledSoftware"/>
</MultiBinding>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=chbx_hideRequiredMissingSoftware, Path=IsChecked}" Value="True">
<Setter Property="Visibility">
<Setter.Value>
<MultiBinding Converter="{StaticResource RequiredMissingSoftwareToVisibiltyConverter}">
<Binding Path="."/>
<Binding ElementName="chbx_hideRequiredMissingSoftware"/>
</MultiBinding>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
<CollectionViewSource x:Key="totalSoftwareCollectionViewSource"
Source="{Binding TotalSoftwareOC}"
CollectionViewType="ListCollectionView"/>
</UserControl.Resources>
<Grid>
<Border Style="{StaticResource UI.BorderInnerStyle}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="1"
Margin="5">
<Label Content="Filters"
Margin="5"
FontWeight="Bold"
HorizontalAlignment="Center"/>
<CheckBox x:Name="chbx_hideUnrequiredSoftware"
Content="Hide unrequired software"
Foreground="White"
IsChecked="{Binding UnrequiredSoftwareIsChecked}"/>
<CheckBox x:Name="chbx_hideRequiredInstalledSoftware"
Content="Hide required installed software"
Foreground="Green"
IsChecked="{Binding RequiredInstalledSoftwareIsChecked}"/>
<CheckBox x:Name="chbx_hideRequiredMissingSoftware"
Content="Hide required missing software"
Foreground="Red"
IsChecked="{Binding RequiredMissingSoftwareIsChecked}"/>
</StackPanel>
<Label Grid.Row="0"
Grid.Column="1"
Margin="5"
Content="Total software"
FontWeight="Bold"
HorizontalAlignment="Center"/>
<DataGrid Grid.Row="1"
Grid.Column="1"
Margin="5"
RowStyle="{StaticResource DataGridRowStyle}"
VirtualizingStackPanel.VirtualizationMode="Standard"
RowHeight="20"
MinRowHeight="0"
IsReadOnly="True"
HeadersVisibility="Column"
ItemsSource="{Binding Source={StaticResource totalSoftwareCollectionViewSource}}"
AutoGenerateColumns="False"
SelectionMode="Single"
SelectionUnit="FullRow"
SelectedItem="{Binding SoftwareSelectedRow}">
<DataGrid.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick"
Command="{Binding DataGridRowDoubleClickCommand}" />
</DataGrid.InputBindings>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}"
Header="Name"/>
<DataGridTextColumn Binding="{Binding Version}"
Header="Version"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Border>
</Grid>