dimanche 15 octobre 2023

How to use one checkbox (IsChecked) to control another checkbox (IsEnabled) by binding function?

I use Visual Studio 2022 to create a WPF application. I have two checkboxes, grade_4_CB and grade_4_centered_CB in a Windows frame like this:

<Window>
    <Grid>
        <Viewbox Stretch=" fill">
            <Canvas Width=" 1100" Height=" 620">
                <CheckBox x:Name="grade_4_CB" Content="Grade 4" />
                <CheckBox x:Name="grade_4_centered_CB" Content="Grade 4 Centered"/> 
            </Canvas>
        </Viewbox>
    </Grid>
</Window>

Now, when I click grade_4_CB (IsChecked="True"), the grade_4_centered_CB's IsEnabled="True".

While not clicking of grade_4_CB (IsChecked="False"), the grade_4_centered_CB's IsEnabled="False"

I tried to add behind grade_4_centered_CB like,

<CheckBox x:Name="grade_4_centered_CB" Content="Grade 4 Centered">
    <CheckBox.Style>
        <Style TargetType="CheckBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=grade_4_CB, Path=IsChecked}" Value="True">
                    <Setter Property="IsEnabled" Value="True" />
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=grade_4_CB, Path=IsChecked}" Value="False">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </CheckBox.Style>
</CheckBox>

However, I get an error,

Error XLS0415: No attachable property 'Style' found on type 'CheckBox'

Another method is replacing <CheckBox.Style> to <CheckBox.Resources> but I get a similar error.

Besides, I also tried to modify directly the property of grade_4_centered_CB in right column (common-IsEnable-data bindingdialog), find and add grade_4_CB -path(IsChecked), but no matter False string or True string, there is no change in grade_4_centered_CB




Aucun commentaire:

Enregistrer un commentaire