Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XAML Binding to the opposite of another element

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.

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)}"/>

Or something like:

                    Path != IsEnabled}"/>

Or even something like:

                    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.

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

like image 333
Francis Rodgers Avatar asked Sep 24 '11 15:09

Francis Rodgers


People also ask

What are the different binding mechanism in XAML?

Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.

What is oneway binding WPF?

In One Way binding, source control updates the target control, which means if you change the value of the source control, it will update the value of the target control. So, in our example, if we change the value of the slider control, it will update the textbox value, as shown below.

What is Ivalueconverter WPF?

ValueConverters.rar. A Value Converter functions as a bridge between a target and a source and it is necessary when a target is bound with one source, for instance you have a text box and a button control. You want to enable or disable the button control when the text of the text box is filled or null.

What are the different data binding modes available in WPF?

<TextBox x:Name="DestinationTextBox" Text="{Binding ElementName=SourceTextBox, Path=Text, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" Margin="{StaticResource MarginTop}" Width="150"


1 Answers

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}"/>

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

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

You can get a more information about IValueConverter from MSDN.

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.

like image 80
vcsjones Avatar answered Oct 06 '22 01:10

vcsjones