tags:

views:

32

answers:

2

I need one button to be enabled only when two other properties have been set to a value other than null. I could to this manually but I wonder if there is a way to do it using .net's Binding class. I'm using .net 4.0 working with Windows Forms.

+1  A: 

Bind your Button.IsEnabled property and make use of an IMultiValueConverter...which you can then return true only when your values are not null.

<Button.IsEnabled>
    <MultiBinding Converter="{StaticResource MyConverter}">
        <Binding Property1 />
        <Binding Property2 />
    </MultiBinding>
</Button.IsEnabled>
Aaron
Sorry I'm not using WPF. My bad.
jsoldi
+2  A: 

No I do not believe this is possible in a WinForms application. A WinForms binding is a 1 to 1 mapping between a source object and property to a data member on the target.

An easy way to work around this though is to create a 3rd property which simply does the check you are trying to make and create a binding to that property.

public object Property1 {get; set;}
public object Property2 {get; set;}
public bool Property3
{
    get { return Property1 != null && Property2 != null; }
}
JaredPar
And then I guess I need to implement a `Property1_or_Property2Changed()` function to let everyone know that it has changed so the button can update it's `Enabled` property?
jsoldi