根据类中的其他一些变量来控制按钮的 IsEnable 属性的方法?

发布于 2024-10-06 09:25:57 字数 1311 浏览 3 评论 0原文

我本质上有一个问题,我有一些按钮的 IsEnabled 属性需要根据类中的一些其他变量/属性进行控制。

所以在 XAML 中我可能有:

<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>

在后面的代码中我可能有:

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();
        }
    }
}

所以,我想要的行为如下:

当A和B之和为5或更大时, 该按钮应该被启用。当它 小于 5,按钮应该是 已禁用。

已经编写了 ButtonEnabledProp 来帮助完成此过程。除了在代码中更改 A 和 B 的每个部分手动设置按钮状态之外,如何实现这一点?

绑定到转换器固然很好,但转换器无法访问 A 或 B。 绑定到属性本身会很好,但我不知道如何执行此操作,也没有找到示例。

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 how.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光礼记 2024-10-13 09:25:57

您应该使 MainWindow 实现 INotifyPropertyChanged 接口,并调用 OnPropertyChanged,并使用“ButtonEnabledProp”作为属性名称:

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));
        }
    }
}

然后您可以修改您的 xaml 文件以向其添加数据绑定,并具有类似以下内容:

<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>

我刚刚粘贴的代码将使您的按钮被启用。

如果您想实现流程自动化,没有简单的方法。但是PostSharp 做得很好。

希望这有帮助

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

舂唻埖巳落 2024-10-13 09:25:57

如果需要使用绑定,最好将此属性放在另一个类上,而不是放在视图本身上。

因此:

  • 创建一个类 (ViewModel)
  • 将此属性添加到类中
  • 在按钮的 IsEnabled 上添加视图的绑定 将
  • 视图的 DataContext 设置为 ViewModel 的实例
  • 实现 INotifyPropertyChanged 接口并引发 <条件更改时发生 code>PropertyChanged 事件。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文