问题
I have a list a simple dialog box which contains a few checkboxes, I wanted to have an Ok button that would be disabled unless the user changed a setting. In my view I have an OkEnabled property that I was binding the isEnabled property of the button to, if a check box changes its value it sets OkEnabled to true, but for some reason this doesnt enable the button.
public bool OkEnabled
{
get
{
return m_okEnabled;
}
set
{
m_okEnabled = value;
OnPropertyChanged("OkEnabled");
}
}
<Button Content="OK" Style="{StaticResource MyButton}" Height="23"
HorizontalAlignment="Left" Margin="20" Name="m_okbutton"
VerticalAlignment="Top" Width="75"
Click="okClick" IsEnabled="{Binding Path=OkEnabled}"/>
For some reason the Ok button won't change state when the OkEnabled property changes state. If I bind the IsEnabled property to one of the checkboxes I can see the button change state as the check box changes.
回答1:
In my view I have an OkEnabled property
By default, bindings are relative to the DataContext, not the view. Did you set the view as its own DataContext ?
回答2:
As you said that OkEnabled property is in view. which is not correct if you are using MVVM. It should be in model and assign model object into datacontext of view.
来源:https://stackoverflow.com/questions/9770947/isenabled-binding-to-a-boolean