2011-12-25 99 views
7
<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。將集合綁定到WPF組合框並禁用某些項目

IpInfo class have a bool property Enabled。要求是設置ComboBoxItem.IsEnabled=false(以便用戶不能選擇它)時相應的IpInfo.Enable==false

我希望所有代碼都是用XAML編寫的。

回答

23
<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財產

+0

大,它的作品! – Gqqnbig 2011-12-30 05:51:39