XAML绑定到另一个元素的相反 [英] XAML Binding to the opposite of another element

查看:119
本文介绍了XAML绑定到另一个元素的相反的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的练习,想知道是否有办法绑定到使用XAML的另一个元素的对面。例如。我有一个表单上的两个按钮,开始和停止也许是一个定时器。我不想同时启用两个。程序启动时,应禁用停止按钮。当点击开始按钮时,应该禁用该按钮并启用停止按钮。反之亦然,直到用户退出。

I am developing a simple exercise and want to know if there is a way to bind to the opposite of another element using only XAML. For example. I have two buttons on a form, Start and Stop perhaps for a timer. I dont want both to be enables at the same time. When the program starts, the stop button should be disabled. When the start button is clicked, it should be disabled and the stop button enabled. Then vice versa until the user quits.

我知道这里有更好的控件,我知道在代码中很容易做到。我只是想知道XAML中是否有某种NOT运算符可能如下所示:

I know there are better controls for exactly this, and I know it is easy to do in code. I just wanted to know if there is some sort of NOT operator in XAML that could look like this:

<Button Content="_Start" Name="btnStart"/>
<Button Content="_Stop" Name="btnStop"
        IsEnabled="{Binding ElementName=btnStart,
                    Path=Not(IsEnabled)}"/>

或类似于:

                    Path != IsEnabled}"/>

甚至可以这样:

                    Path=IsEnabled.Not()}"/>

我知道我可以直接绑定到开始按钮,但是当一个启用时是另一个,同样当它被禁用时。看看我从哪里来的。

I know I can bind directly to the start button, but when one is enabled so is the other, and likewise when it is disabled. See where I am comming from.

我甚至会乐意接受某种XAML验证,它将首先检查开始按钮的状态,并强制执行停止一个是相反的。或任何其他类似的黑客或解决方法,不需要代码保护。

I would be even willing to entertain some sort of XAML Validation thehnique which will first check the state of the start button and force the state of the stop one to be the opposite. Or any other similar hacks or workarounds that dont require codebehind.

感谢您的任何帮助。

EDITS :
记住这个关键点是没有代码背后。如果我必须编写一行代码,那么我也可以使用onclick事件处理程序和一个简单的?:运算符。所以我只想要在XAML中直接工作的东西。

EDITS: Remember the key point for this is NO code behind. If I have to write a single line of code behind I might as well just use the onclick event handler and a simple ?: operator. So I want only something that will work directly in XAML.

推荐答案

没有一个 / code>开箱即用的操作。但是,使用自定义转换器实现这一点很容易:

There isn't a Not operation out of the box. However, it's pretty easy to achieve this yourself using a custom converter:

public class NotConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return !(bool) value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return !(bool)value;
    }
}

然后,您可以将其指定为绑定的转换器

Then you can specify it as a converter on your binding.

<Button Content="_Stop" Name="btnStop"
    IsEnabled="{Binding ElementName=btnStart, Converter={StaticResource NotConverter} Path=IsEnabled}"/>

最后在您的Window /页面上创建一个名为NotConverter的资源:

And finally create a resource on your Window / Page called NotConverter:

<Window.Resources>
    <ns:NotConverter x:Key="NotConverter" />
</Window.Resources>

您可以获得有关的更多信息IValueConverter 来自 MSDN

然后使用可以在任何你想要的地方使用这个值转换器。您可以将其从Window资源中拉出,并将其作为应用程序级资源,因此您不必在每个窗口中指定它。

Then use can use this value converter anywhere you'd like. You could pull it out of the Window resources and make it a application-level resource so you don't have to specify it in every window.

这篇关于XAML绑定到另一个元素的相反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆