wpf IsEnabled button binding, only works once....already have PropertyChange notification

时间:2016-10-20 13:10:01

标签: c# wpf visual-studio xaml binding

here is my xaml:

<Button 
 x:Name="StartBtn" 
 Content="Start"
 Command="{Binding StartProcess}"
 HorizontalAlignment="Left"
 Height="23" 
 Margin="241,181,0,0" 
 VerticalAlignment="Top"
 Width="57" 
 Grid.Column="1" 
 IsEnabled="{Binding IsStartButtonEnabled}"/>

here is my property:

 private bool _isStartButtonEnabled;
        public bool IsStartButtonEnabled
        {
            get { return _isStartButtonEnabled; }
            set
            {
                _isStartButtonEnabled = value;
                OnPropertyChanged("IsStartButtonEnabled");
            }
        }

it works with whatever i set it to on startup, but when i manipulate the bool later in the code, the change is not reflected in the WPF. I do have the data context set correctly as other bindings all function perfectly.

Also when I manipulate the Bool in the code I am using the property and not the private field.

private void OnStartProcess()
        {
            IsStartButtonEnabled = false;
            //stuff
        }

1 个答案:

答案 0 :(得分:0)

It is working, make sure you are setting the public property in your code and not the private field:

   public RelayCommand StartProcess
    {
        get
        {
            return new RelayCommand(Execute);
        }
    }

    private void Execute()
    {
        IsStartButtonEnabled = false;
    }