Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding a collection to WPF ComboBox and disable some items

Tags:

binding

xaml

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

like image 756
Gqqnbig Avatar asked Dec 25 '11 06:12

Gqqnbig


1 Answers

<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

like image 140
chopikadze Avatar answered Oct 20 '22 20:10

chopikadze