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

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

这段代码将App.IpInfoList与ComboBox绑定。

IpInfo类有一个bool类型的属性Enabled。要求是当相应的IpInfo.Enable==false时,设置ComboBoxItem.IsEnabled=false(这样用户就无法选择它)。

我希望所有的代码都写在XAML中。

1个回答

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

它将 ComboBoxItem.IsEnabled 属性绑定到您的 IpInfo.Enabled 属性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接