Binding a collection to WPF ComboBox and disable some items

浪尽此生 提交于 2019-12-20 21:30:12

问题


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


回答1:


<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



来源:https://stackoverflow.com/questions/8628554/binding-a-collection-to-wpf-combobox-and-disable-some-items

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