IsEnabled binding to a boolean

大城市里の小女人 提交于 2019-12-11 06:46:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!