开发者

Ways to control the IsEnable property of a button according to some other variables within a class?

I essentially have a problem where I have some button's who's IsEnabled porperty needs to be controlled based on some other variables/properties within the class.

So in the XAML I might have:

<Window x:Class="RoboticPainterGUI.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:RoboticPainterGUI"
        Loaded="Window_Loaded" Name="Window_Main"
        Title="MainWindow" Height="555" Width="630">
    <Grid>
        <Button Name="Button_Next" Content="Lorem Ipsum" />
    </Grid>
</Window>

And in the code-behind for this I might have:

namespace RoboticPainterGUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        int A, B;

        bool ButtonEnabledProp
        {
            get
            {
                return ((A + B) >= 5);
            }
        }

        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

So, what the behavior I want is as follows:

When the sum of A and B is 5 or more, the button should be enabled. When it is less than 5, the button should be disabled.

The ButtonEnabledProp has been written already to aid in this process. How can this be implemented other than manually setting the button's state at every part in the code where A and B are changed?

Binding to a converter would be nice, but the converter wouldn't have access to A or B. Binding to the property itself would be nice, but I don't know how to do this and haven't found an example ho开发者_如何学Cw.


You should make MainWindow implement the INotifyPropertyChanged interface, and call OnPropertyChanged, with "ButtonEnabledProp" as property name:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    int A, B;

    bool ButtonEnabledProp
    {
        get
        {
            return ((A + B) >= 5);
        }
    }

    public MainWindow()
    {
        InitializeComponent();

        A = B = 3;

        NotifyPropertyChanged("ButtonEnabledProp");
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

Then you could modify your xaml file to add databinding to it, and have something like:

<Window x:Class="RoboticPainterGUI.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:RoboticPainterGUI"
        Loaded="Window_Loaded" Name="Window_Main"
        Title="MainWindow" Height="555" Width="630">
    <Grid>
        <Button Name="Button_Next" Content="Lorem Ipsum" IsEnabled="{Binding Path=ButtonEnabledProp}"  />
    </Grid>
</Window>

The code I've just pasted would make your button to be enabled.

If you want to automate your process, there is no easy way. But PostSharp does this very well.

Hope this helps


If you need to use binding, you better put this property on another class rather than on the view itself.

So:

  • Create a class (ViewModel)
  • Add this property to the class
  • Add the binding for the View on the IsEnabled of the button
  • Set DataContext of the View to an instance of the ViewModel
  • Implement INotifyPropertyChanged interface and raise PropertyChanged event when condition is changed.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜