简体   繁体   English

ComboBox IsEnabled 绑定问题在 Silverlight Xaml

[英]ComboBox IsEnabled Binding Question in Silverlight Xaml

I have a ComboBox whose xaml is as follows我有一个 ComboBox 其 xaml 如下

<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转换器获取 Items.Count 并检查它是否大于 0 如果大于 0 然后启用它否则禁用它

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 )目标是启用 ComboBox 如果 ComboBox 只有在它有项目时才禁用它,(自我绑定到它的 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现在上面的绑定给了我一个错误

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 .由于我不明白的原因,在 Silverlight 上,转换器看到的valuedouble类型,但应该是int In fact it is an int on WPF.实际上它是 WPF 上的一个int

But since this is the case, just processing it as a double fixed the problem:但既然是这种情况,只需将其作为double处理即可解决问题:

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.这对我来说似乎很麻烦,但 Silverlight 有时是一个奇怪的野兽。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM