绑定集合WPF组合框和禁用某些项目 [英] Binding a collection to WPF ComboBox and disable some items

查看:166
本文介绍了绑定集合WPF组合框和禁用某些项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < Window.Resources>
    <的DataTemplate X:键=IpInfoTemplate>
        <&DockPanel中GT;
            < TextBlock的文本={绑定路径= InterfaceName中}DockPanel.Dock =左保证金=0,0,10,0/>
            < TextBlock的文本={绑定路径=地址}/>
        < / DockPanel中>
    < / DataTemplate中>
< /Window.Resources><组合框的ItemTemplate ={StaticResource的IpInfoTemplate}
      的ItemsSource ={绑定源= {X:静态WpfApplication1:App.IpInfoList},模式=单向}>
< /组合框>

这code已经绑定 App.IpInfoList 来组合框。

IpInfo 类有一个布尔属性已启用。要求是集 ComboBoxItem.IsEnabled = FALSE (使用户无法选择它),对应时 IpInfo.Enable ==虚假

我希望所有code被写入在XAML。


解决方案

 <组合框的ItemTemplate ={StaticResource的IpInfoTemplate}
          的ItemsSource ={绑定源= {X:静态WpfApplication1:App.IpInfoList},模式=单向}>
    < ComboBox.ItemContainerStyle>
        <风格的TargetType ={X:类型ComboBoxItem}>
            < setter属性=IsEnabledVALUE ={结合有效}/>
        < /样式和GT;
    < /ComboBox.ItemContainerStyle>
< /组合框>

它结合 ComboBoxItem.IsEnabled 属性为你的 IpInfo.Enabled 属性

<Window.Resources>
    <DataTemplate x:Key="IpInfoTemplate">
        <DockPanel>
            <TextBlock Text="{Binding Path=InterfaceName}" DockPanel.Dock="Left" Margin="0,0,10,0" />
            <TextBlock Text="{Binding Path=Address}"/>
        </DockPanel>
    </DataTemplate>
</Window.Resources>

<ComboBox ItemTemplate="{StaticResource IpInfoTemplate}"
      ItemsSource="{Binding Source={x:Static WpfApplication1:App.IpInfoList}, Mode=OneWay}">    
</ComboBox>

This code has binded App.IpInfoList to ComboBox.

IpInfo class has a bool property Enabled. The requirement is that set ComboBoxItem.IsEnabled=false (so that users can't select it) when corresponding IpInfo.Enable==false.

I hope all code is written in XAML.

解决方案

<ComboBox ItemTemplate="{StaticResource IpInfoTemplate}" 
          ItemsSource="{Binding Source={x:Static WpfApplication1:App.IpInfoList}, Mode=OneWay}">
    <ComboBox.ItemContainerStyle>
        <Style TargetType="{x:Type ComboBoxItem}">
            <Setter Property="IsEnabled" Value="{Binding Enabled}"/>
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>

It binds ComboBoxItem.IsEnabled property to your IpInfo.Enabled property

这篇关于绑定集合WPF组合框和禁用某些项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆