ToggleButton wpf IsEnabled property cannot binding to datasource

华 陈 1 Reputation point
2022-02-14T09:05:48.247+00:00

It is a so strange problem,
I am try to binding IsEnabled to a datasource property. like
IsEnabled="{Binding CanLockUnlock, Mode=OneWay}">
CanLockUnlock has implemented propertychanged..
It cannot work, but when I attached by VS debug to the application, and remove 'Mode=OneWay', the binding works!

when I write IsEnabled="{Binding CanLockUnlock}"> It also cannot work by itself !, the odd thing happens,
I attached to application by VS debug. add 'Mode=OneWay' , the binding works !!!!

that is whatever I write the binding, it doesn't work by itself.. only when I revised the binding by attaching it ,change the binding setting, it work!

I find it ToggleButton wpf IsEnabled property cannot binding..

Someone help me !!

I have made a workround to the problem, I put the ToggleButton in a stackpanel and binding IsEnabled of stackpanel.
I still want to know why...ToggleButton cannot works

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,773 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,321 Reputation points
    2022-02-15T07:52:12.907+00:00

    Hi,
    there's no problem. Try following demo:

    XAML:

    <Window x:Class="Window111"  
            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:WpfApp1.WpfApp111"  
            mc:Ignorable="d"  
            Title="62241333_220215" Height="450" Width="800">  
      <Window.DataContext>  
        <local:ViewModel/>  
      </Window.DataContext>  
        <StackPanel>  
        <Button Content="Change CanLockUnlock" Command="{Binding}" Margin="10"/>  
        <ToggleButton Content="ToggleButton" IsEnabled="{Binding CanLockUnlock}" Margin="10"/>  
      </StackPanel>  
    </Window>  
    

    and ViewModel:

    Imports System.ComponentModel  
    Imports System.Runtime.CompilerServices  
      
    Namespace WpfApp111  
      Public Class ViewModel  
        Implements ICommand, INotifyPropertyChanged  
      
        Public Property CanLockUnlock As Boolean = True  
      
        Public Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged  
        Public Sub Execute(parameter As Object) Implements ICommand.Execute  
          CanLockUnlock = Not CanLockUnlock  
          OnPropertyChanged(NameOf(CanLockUnlock))  
        End Sub  
        Public Function CanExecute(parameter As Object) As Boolean Implements ICommand.CanExecute  
          Return True  
        End Function  
      
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged  
        Private Sub OnPropertyChanged(<CallerMemberName> Optional propName As String = "")  
          RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))  
        End Sub  
      End Class  
    End Namespace  
    

    Result:

    174401-x.gif

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.