Karim Wahib
Karim Wahib

Reputation: 21

Validating a ListBoxItem rather than a ListBox INotifyDataErrorInfo

I tried Solutions Here : Validating a ListBoxItem rather than a ListBox and WPF ListBox ErrorTemplate and WPF INotifyDataErrorInfo highlight ListBoxItem

I have a ListBox

<ListBox ItemsSource="{Binding ViewNewPanelsPairs}" ItemTemplate="{StaticResource LevelsTemplate}"  Style="{StaticResource PanelsListBox}"
                     SelectionMode="Single"  Margin="10" SelectedItem="{Binding CurrentViewNewPanelsPair, ValidatesOnNotifyDataErrors=True}" ItemContainerStyle="{StaticResource LevelItemTemplate}"/>

With the following item templates and itemcontainerstyle

<DataTemplate x:Key="LevelsTemplate" DataType="{x:Type VM:ViewNewPanelsPair}">
    <CheckBox VerticalContentAlignment="Center"  Content="{Binding View.Name}"   
              IsChecked="{Binding IsViewPanelsNotEmpty, Mode=OneWay}"
              IsHitTestVisible="False"
              IsEnabled="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}"/>
</DataTemplate>



<Style x:Key="LevelItemTemplate" TargetType="ListBoxItem" >
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="BorderBrush" Value="Transparent" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding Validation.HasErrors}" Value="True">
            <Setter Property="BorderBrush" Value="Red"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

and following validations (implementing INotifyDataErrorInfo)

public ViewNewPanelsPair CurrentViewNewPanelsPair
    {
        get => currentViewNewPanelsPair;
        set
        {
            Set(() => CurrentViewNewPanelsPair, ref currentViewNewPanelsPair, value);
            ValidateProperty(nameof(CurrentViewNewPanelsPair));
        }
    }

and

protected void ValidateProperty(string PropertyName)
    {
        ClearErrors(PropertyName);
        switch (PropertyName)
        {
            case nameof(ViewNewPanelsPairs):
                if (!ViewNewPanelsPairs.Any(x => x.IsViewPanelsNotEmpty))
                {
                    AddError(PropertyName, "At least one view must have new panels");
                }
                break;
            case nameof(CurrentViewNewPanelsPair):
                if (CurrentViewNewPanelsPair.NewPanels.Count != 0 && CurrentViewNewPanelsPair.NewPanels.Count!=ViewNewPanelsPair.OldPanels.Count)
                {
                    AddError(PropertyName, "New Panels must be equal to Old Panels");
                }
                break;
        }
    }

No matter how hard i try i always get the whole listbox highlighted surrounded with red border on error (not the listboxitem that has the error) Is there something wrong with my code ??

Note : i debugged the code to make sure that there is actually error and there was and it shows it as a border around the whole listbox

Upvotes: 0

Views: 219

Answers (1)

Karim Wahib
Karim Wahib

Reputation: 21

It worked when i implemented INotifyDataErrorinfo on my model class "ViewNewPaanelsPair" and bound the datatrigger of itemscontrolcontainer to the property that raises the error like this

<DataTrigger Binding="{Binding NewPanels}" > <Setter Property="BorderBrush" Value="Red"/> </DataTrigger>

Thanks @Andy for your help

Upvotes: 0

Related Questions