Click here to Skip to main content
15,921,716 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi.
I need to bind a ToggleButton to a Boolean. So when user clicks it, a MessageBox appears and shows the situation of it.
I tried bellow code, but it doesn't work!
Anybody kindly can help me please on this?

What I have tried:

VB.NET
Public Class BooleanWrapper
    Implements ComponentModel.INotifyPropertyChanged
    Public Event PropertyChanged As ComponentModel.PropertyChangedEventHandler Implements ComponentModel.INotifyPropertyChanged.PropertyChanged

    Private _IsScrollList As Boolean

    Public Property IsScrollList As Boolean
        Get
            Return _IsScrollList
        End Get
        Set(value As Boolean)

            If _IsScrollList <> value Then
                _IsScrollList = value
            End If
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("IsScrollList"))
            MsgBox(IsScrollList)

        End Set
    End Property

End Class


XML
<ToggleButton x:Name="TglBtn"
              DataContext="BooleanWrapper"
              IsChecked="{Binding IsScrollList}"/>
Posted
Updated 26-May-24 9:30am
v2

1 solution

You need to use two-way binding. You should know this by now.

Here is an articile that not only shows you how, but also adds a modern look to the toggle button: Flexible WPF ToggleSwitch Lookless Control in C# & VB[^]
 
Share this answer
 
Comments
Sh.H. 25-May-24 18:34pm    
@Graeme_Grant
Thanks. I read that article, but no luck!
Graeme_Grant 25-May-24 18:45pm    
PropertyChanged event is not raised in the Property Setter.
Sh.H. 25-May-24 19:25pm    
@Graeme_Grant
It is in 4th line of vb code!!!!
I don't understand.
Would you please kindly correct my code as new answer?
Thanks in advance respectfully.
Graeme_Grant 25-May-24 22:38pm    
We have answered this multiple times in the past. You need to put some effort in.

All that you need is in the article linked above. Download the code, run it, set breakpoints, and learn how it works.
Dave Kreskowiak 25-May-24 23:51pm    
You've seen this over and over again in other posts. In your Property Set method, you MUST raise the PropertyChanged event, otherwise any bound controls will never be notified your property changed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900