c# - 将 checkbox.ischecked 绑定(bind)到 bool 属性?

标签 c# vb.net xaml

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

最佳答案

您可以使用 WPF 数据绑定(bind):
无需使用 if 更改它,只需绑定(bind) XAML 接口(interface)并更改类的属性即可。

Imports System.ComponentModel

Public Class Class1
    Implements INotifyPropertyChanged
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Private _ModeSta1 As Boolean
    Property ModeSta1 As Boolean
        Get
            Return _ModeSta1
        End Get
        Set(ByVal value As Boolean)
            _ModeSta1 = value
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(NameOf(ModeSta1)))
        End Set
    End Property

End Class

将 CheckBox 更改为仅绑定(bind)到 ModeSta1 属性

<CheckBox x:Name="ckbx1" 
    Content=" Input" 
    Margin="99.866,78.932,0,0" 
    VerticalAlignment="Top" 
    HorizontalAlignment="Left" 
    IsChecked="{Binding Path=ModeSta1, Mode=TwoWay}" 
/>

ModeSta1 的值更改时,用户界面会更新,如果用户单击复选框,ModeSta1 的值也会更改。

    Dim c = New Class1
    DataContext = c
    ' You can change the property and this change will be visible on the UI
    c.ModeSta1 = True

CheckBox

关于c# - 将 checkbox.ischecked 绑定(bind)到 bool 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50936300/

相关文章:

c# - VB.Net 'Overridable' 对成员变量声明无效

c# - WPF XAML - DataTriggers 或 ValueConverters?最佳实践

WPF Grid.Resources 样式打破了 ResourceDictionary 样式。如何让它们共存?

c# - 访问列表框中的数据

c# - WebJob 控制台应用程序中的 ProcessQueueMessage 函数

c# - 将 Dictionary<MyType>.ValueCollection 转换为 IList<MyType>

mysql - 这是通过 CellEndEdit 在 datagridview 中更新数据库的唯一方法吗?

vb.net - 如何将一串键/值对转换为 HashTable 或 Dictionary 或者?

C#命令行反编译器

silverlight - 嵌套的 silverlight 转换的执行顺序是什么?