将集合绑定到WPF ComboBox并禁用某些项目

Gqq*_*big 8 xaml binding

<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>
Run Code Online (Sandbox Code Playgroud)

此代码绑定App.IpInfoList到ComboBox.

IpInfoclass有bool属性Enabled.要求是ComboBoxItem.IsEnabled=false在相应时设置(以便用户不能选择它)IpInfo.Enable==false.

我希望所有代码都是用XAML编写的.

cho*_*dze 26

<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>
Run Code Online (Sandbox Code Playgroud)

它将ComboBoxItem.IsEnabled财产绑定到您的IpInfo.Enabled财产