猿问

将 checkbox.ischecked 绑定到布尔属性?

可有人比我聪明请什么应该是一个简单的编码任务的帮助:绑定.ischecked复选框的一个对象的布尔属性?我创建了一个简单的项目来模拟我在一个更大的项目中尝试做的事情,以进行简单的调试和演示。我有一个复选框,单击该复选框时会设置我的类对象的布尔属性。UI 上的另一个复选框应根据该布尔属性的 T/F 值更新其选中状态。这里有什么问题以及如何修复并使其正常工作?我“相信”这个问题可能是因为我设置的对象的属性与 checkbox.ischecked 属性绑定到的对象不同(???)。这是问题吗,如果是,我该如何解决?VB或XAML端需要实现哪些代码?


主窗口.xaml.vb


Public Bools As New Boolean_Properties


Private Sub ckbx1_Click(sender As Object, e As RoutedEventArgs) Handles ckbx1.Click

    If ckbx1.IsChecked Then

        Bools.Sta1Mode = True

    Else Bools.Sta1Mode = False

    End If

End Sub

Boolean_Properties 类


Implements INotifyPropertyChanged

Private ModeSta1 As Boolean

Public Event ThePropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged


Property Sta1Mode As Boolean

    Get

        Return ModeSta1

    End Get

    Set(ByVal value As Boolean)

        ModeSta1 = value

        RaiseEvent ThePropertyChanged(Me, New PropertyChangedEventArgs("Sta1Mode"))

    End Set

End Property

XAML


<Window x:Class="MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    xmlns:local="clr-namespace:BindBooleanProperty"

    mc:Ignorable="d"

    xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"

    Title="MainWindow" Height="217.032" Width="226.067">

<Window.DataContext>

    <local:Boolean_Properties/>

</Window.DataContext>

<Grid>

    <CheckBox Content="Output" HorizontalAlignment="Left" Margin="99.866,134.399,0,0" VerticalAlignment="Top" IsChecked="{Binding Sta1Mode, Mode=OneWay}">

        <CheckBox.DataContext>

            <local:Boolean_Properties/>

        </CheckBox.DataContext>

    </CheckBox>

    <CheckBox x:Name="ckbx1" Content=" Input" HorizontalAlignment="Left" Margin="99.866,78.932,0,0" VerticalAlignment="Top"/>


</Grid>


慕哥6287543
浏览 356回答 1
1回答

蓝山帝景

您可以使用 WPF 数据绑定:无需使用 更改它if,只需绑定 XAML 接口并更改类上的属性即可。Imports System.ComponentModelPublic Class Class1&nbsp; &nbsp; Implements INotifyPropertyChanged&nbsp; &nbsp; Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged&nbsp; &nbsp; Private _ModeSta1 As Boolean&nbsp; &nbsp; Property ModeSta1 As Boolean&nbsp; &nbsp; &nbsp; &nbsp; Get&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Return _ModeSta1&nbsp; &nbsp; &nbsp; &nbsp; End Get&nbsp; &nbsp; &nbsp; &nbsp; Set(ByVal value As Boolean)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _ModeSta1 = value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(NameOf(ModeSta1)))&nbsp; &nbsp; &nbsp; &nbsp; End Set&nbsp; &nbsp; End PropertyEnd Class将 CheckBox 更改为仅绑定到ModeSta1属性<CheckBox x:Name="ckbx1"&nbsp;&nbsp; &nbsp; Content=" Input"&nbsp;&nbsp; &nbsp; Margin="99.866,78.932,0,0"&nbsp;&nbsp; &nbsp; VerticalAlignment="Top"&nbsp;&nbsp; &nbsp; HorizontalAlignment="Left"&nbsp;&nbsp; &nbsp; IsChecked="{Binding Path=ModeSta1, Mode=TwoWay}"&nbsp;/>当 的值ModeSta1改变时,用户界面会更新,如果用户点击复选框,ModeSta1值也会改变。&nbsp; &nbsp; Dim c = New Class1&nbsp; &nbsp; DataContext = c&nbsp; &nbsp; ' You can change the property and this change will be visible on the UI&nbsp; &nbsp; c.ModeSta1 = True
随时随地看视频慕课网APP
我要回答