3 c# silverlight xaml binding combobox
我有一个ComboBox,其xaml如下
<ComboBox Name="ComboBoxDiscussionType" IsEnabled="{Binding ElementName=ComboBoxDiscussionType, Path=Items.Count, Converter={StaticResource ComboBoxItemsCountToBoolConverter}}"/>
Run Code Online (Sandbox Code Playgroud)
转换器获取Items.Count并检查它是否大于0如果大于0然后启用它,否则禁用它
目标是启用ComboBox,如果ComboBox只有它有项目,否则禁用它,(自我绑定到其item.count)
以下是我的转换器,
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我该如何实现呢?现在上面的Binding给了我一个错误
由于我不理解的原因,在Silverlight上value
,转换器看到的类型double
应该是int
.事实上它是一个int
WPF.
但既然如此,只需将其作为一个double
固定的问题处理:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (int)(double)value > 0;
}
Run Code Online (Sandbox Code Playgroud)
奇怪的是,更传统的相对源绑定也不起作用:
<Binding RelativeSource="{RelativeSource Self}" .../>
Run Code Online (Sandbox Code Playgroud)
但是您的原始元素名称绑定会:
<ComboBox Name="ComboBoxDiscussionType" IsEnabled="{Binding ElementName=ComboBoxDiscussionType, Path=Items.Count, Converter={StaticResource ComboBoxItemsCountToBoolConverter}}"/>
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎很麻烦,但有时候Silverlight是一个奇怪的野兽.
归档时间: |
|
查看次数: |
1370 次 |
最近记录: |