Reputation: 305
Can someone smarter than me please help with what should be a simple coding task: Binding .ischecked of a checkbox to a boolean property of an object? I have created a simple project to mimic what I'm trying to do in a larger project for simple debug and demonstration. I have a checkbox that when clicked sets a boolean property of my class object. Another checkbox on the UI should update its checked state based on the T/F value of that boolean property. What is wrong here and how to fix and just make it work? I "believe" the problem may be because I'm setting the property of a different object than what the checkbox.ischecked property is bound to (???). Is that the problem, and if so how do I remedy that? What code on the VB or XAML side needs to be implemented?
MainWindow.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
Class 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>
Upvotes: 4
Views: 2359
Reputation: 17637
You can use WPF Data Binding:
Instead of changing it using if
, just bind the XAML interface and change the properties on the class.
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
Change the CheckBox to just Bind to the ModeSta1
property
<CheckBox x:Name="ckbx1"
Content=" Input"
Margin="99.866,78.932,0,0"
VerticalAlignment="Top"
HorizontalAlignment="Left"
IsChecked="{Binding Path=ModeSta1, Mode=TwoWay}"
/>
When the value of ModeSta1
is changed, the User Interface is updated, and if user clicks the checkbox, the ModeSta1
value is changed as well.
Dim c = New Class1
DataContext = c
' You can change the property and this change will be visible on the UI
c.ModeSta1 = True
Upvotes: 4