2011-12-25 32 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> 

Mã này đã gắn App.IpInfoList vào ComboBox.Binding bộ sưu tập vào ComboBox WPF và vô hiệu hóa một số mặt hàng

IpInfo lớp học có thuộc tính bool Enabled. Yêu cầu là đặt ComboBoxItem.IsEnabled=false (để người dùng không thể chọn nó) khi tương ứng IpInfo.Enable==false.

Tôi hy vọng tất cả các mã được viết bằng XAML.

Trả lời

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> 

Nó liên kết ComboBoxItem.IsEnabled sở hữu đối với tài sản của bạn IpInfo.Enabled

+0

Tuyệt vời, nó hoạt động! – Gqqnbig

Các vấn đề liên quan