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

查看:142
本文介绍了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.

感谢您对此的任何帮助。

Thanks for any help on this.

EDITS :
记住关键点,这是NO代码背后。如果我必须写一行代码,我可能只是使用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}"/>

最后在您的窗口/页面上创建一个名为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天全站免登陆