开发者

ComboBox IsEnabled Binding Question in Silverlight Xaml

开发者 https://www.devze.com 2023-03-12 09:11 出处:网络
I have a ComboBox whose xaml is as follows <ComboBox Name=\"ComboBoxDiscussionType\" IsEnabled=\"{Binding ElementName=ComboBoxDiscussionType, Path=Items.Count, Converter={StaticResource ComboBoxIt

I have a ComboBox whose xaml is as follows

<ComboBox Name="ComboBoxDiscussionType" IsEnabled="{Binding ElementName=ComboBoxDiscussionType, Path=Items.Count, Converter={StaticResource ComboBoxItemsCountToBoolConverter}}"/>

and the converter takes the Items.Count and checks if its greater than 0 if its greater than 0 then enable it else disable it

The goal is enable the ComboBox if the ComboBox only if it has Items in it else disable it, ( self Binding to its item.count )

the f开发者_运维问答ollowing is my converter,

public class ComboBoxItemsCountToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (int)value > 0;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

how do i achieve it? right now the above Binding gives me an error


For reasons that I don't understand, on Silverlight the value as seen by the converter is of type double but it should be int. In fact it is an int on WPF.

But since this is the case, just processing it as a double fixed the problem:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    return (int)(double)value > 0;
}

Strangely enough, a more traditional relative source binding doesn't work either:

<Binding RelativeSource="{RelativeSource Self}" .../>

but your original element name binding does:

<ComboBox Name="ComboBoxDiscussionType" IsEnabled="{Binding ElementName=ComboBoxDiscussionType, Path=Items.Count, Converter={StaticResource ComboBoxItemsCountToBoolConverter}}"/>

This seems pretty buggy to me, but Silverlight is a strange beast sometimes.

0

精彩评论

暂无评论...
验证码 换一张
取 消