ComboBox IsEnabled Binding Question in Silverlight Xaml

﹥>﹥吖頭↗ 提交于 2020-01-02 08:07:20

问题


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 following 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


回答1:


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.



来源:https://stackoverflow.com/questions/6301034/combobox-isenabled-binding-question-in-silverlight-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!