简体   繁体   中英

How to re-enable a checkbox in a listview with binding in WPF?

I have a multi-trigger for my IsEnabled and IsChecked property. It is working well except that after the checkbox is disabled, it will not re-enable with code. Here is the XAML:

<ListView ScrollViewer.ScrollChanged="lstRewardsEarned_ScrollChanged" Height="178" HorizontalAlignment="Left" Margin="12,0,0,81" Name="lstRewardsEarned" VerticalAlignment="Bottom" Width="327" SelectionMode="Multiple" Background="White" FontFamily="Calibri" FontSize="16">
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">

            <!-- bind content IsSelected to the ListViewItem IsChecked -->
            <Setter Property="IsSelected" Value="{Binding Path=IsChecked, Mode=TwoWay, UpdateSourceTrigger=Explicit}" />

            <!-- bind content IsEnabled to the ListViewItem IsEnabled -->
            <Setter Property="IsEnabled" Value="{Binding Path=IsEnabled, Mode=TwoWay, UpdateSourceTrigger=Explicit}" />

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border x:Name="Border" SnapsToDevicePixels="true">
                            <ContentPresenter />
                        </Border>

                        <ControlTemplate.Triggers>

                            <!--Highlight if IsSelected and IsEnabled-->
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="True"/>
                                    <Condition Property="IsEnabled" Value="True"/>
                                </MultiTrigger.Conditions>
                                <MultiTrigger.Setters>
                                    <Setter TargetName="Border" Property="Background" Value="Cyan"/>
                                </MultiTrigger.Setters>
                            </MultiTrigger>

                            <!--Remove highlight if is not Selected and IsEnabled-->
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="False"/>
                                    <Condition Property="IsEnabled" Value="True"/>
                                </MultiTrigger.Conditions>
                                <MultiTrigger.Setters>
                                    <Setter TargetName="Border" Property="Background" Value="Transparent"/>
                                </MultiTrigger.Setters>
                            </MultiTrigger>

                            <!--Add a trigger to remove the highlighting if IsEnabled is changed to false-->
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter TargetName="Border" Property="Background" Value="Transparent"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

This is the code to re-enable the checkboxes:

For Each checkBox As CheckBox In lstRewardsEarned.Items
    checkBox.IsEnabled = true

    'This writes out false!
    Console.WriteLine(checkBox.IsEnabled)
Next

Thanks in advance.

Take the UpdateSourceTrigger out of the IsEnabled and IsSelected property bindings?

According to MSDN

If you set the UpdateSourceTrigger value to Explicit, you must call the UpdateSource method or the changes will not propagate back to the source.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM