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.
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; }
}
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>
精彩评论