WPF ComboBox条件ItemTemplate

时间:2013-12-19 07:13:18

标签: c# wpf combobox

我有一个ComboBox绑定到可能值的列表是ViewModel。

<ComboBox ItemsSource="{Binding PossibleOperands}" SelectedValue="{Binding Operand, Mode=TwoWay}" VerticalAlignment="Center"  Foreground="Black"  FontSize="13" FontFamily="Calibri"  Height="23" Grid.Column="1" Margin="7,2,0,2"/>

对于大多数值,我希望显示一个简单的字符串(在上面的示例中,“操作数名称”), 但对于其中一个值,我想显示一个字符串+ 2个组合框。

样机: enter image description here

3 个答案:

答案 0 :(得分:1)

按照MSDN

中的说明使用ItemTemplateSelector课程

答案 1 :(得分:0)

您可以使用文本块和2个组合框定义单个ItemTemplate。使用所需属性绑定组合框的可见性。这种方式仅适用于某些项目,可以看到ComboBox。

答案 2 :(得分:0)

之前我做过类似的事情: 假设你有这样一个类:(对于ComboBox的内容)

    public class Content
    {
      private String _Texty;
      public String Texty
      {
        get{return _Texty;}
        set { _Texty = value; }
      }
      public List<String> Comb1{get ; set;}
      public List<String> Comb2 { get; set; }
      public Content(string t, List<String> comb1, List<String> comb2)
      {
        Texty = "Some Text";
        Comb1 = comb1;
        Comb2 = comb2;
      } 
    }

您需要{x}在xaml部分中定义的Converter

    <utils:ContentToVisibleConverter x:Key="MyConverter" /> 

其中utils类似于:

    xmlns:utils="clr-namespace:YOUR CONVERTER CLASS NAMESPACE"

所以,在xaml中定义你的Combobox

    <ComboBox x:Name="Combo" Margin="4" 
    Height="23" 
    Width="250"  ItemsSource="{Binding Collection}" >
        <ComboBox.ItemTemplate> 
            <DataTemplate> 
                <StackPanel Orientation="Horizontal" >   
                    <TextBlock Text="{Binding Texty}" Margin="4,0"/>

                    <Grid Margin="20 0 0 0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0" Text="Entity Type:" 
                                   Visibility="{Binding Path=Comb1, Converter={StaticResource MyConverter}}" />
                        <ComboBox  Grid.Column="1" ItemsSource="{Binding Comb1}" Width="80" Margin="4,0"
                                   Visibility="{Binding Path=Comb1, Converter={StaticResource MyConverter}}"/>
                        <TextBlock Grid.Column="2" Text="Entity:" 
                                   Visibility="{Binding Path=Comb2, Converter={StaticResource MyConverter}}"/>
                        <ComboBox  Grid.Column="3" ItemsSource="{Binding Comb2}" Width="80" Margin="4,0"
                                   Visibility="{Binding Path=Comb2, Converter={StaticResource MyConverter}}"/>
                    </Grid>

                </StackPanel> 
            </DataTemplate> 
        </ComboBox.ItemTemplate>
    </ComboBox>

在我的代码中,我以这种方式使用Binding集合:

    private ObservableCollection<Content> _Collection;
     public ObservableCollection<Content> Collection
     {
        get { return _Collection; }
        set { _Collection = value; NotifyPropertyChanged("Collection"); }
    }
    // Fill it like this:
      Collection = new ObservableCollection<Content>(); 
      Collection.Add(new Content("Some Stuff", null, null));
      Collection.Add(new Content("Some Stuff", null, null));
      Collection.Add(new Content("Some Stuff", null, null));
      Collection.Add(new Content("Some Stuff",
                       new List<String>() { "One", "Two" },
                       new List<String>() { "One", "Two" }));

最后,我为它只包含的转换器定义了一个类:

    public class ContentToVisibleConverter : System.Windows.Markup.MarkupExtension, IValueConverter
   {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var s = value as List<string>;
        if (s != null)
            return s.Count > 0 ? Visibility.Visible : Visibility.Collapsed;
        else return Visibility.Collapsed;
    }
    public object ConvertBack(object value, Type targetType, object parameter,  System.Globalization.CultureInfo culture)
    {
        return null;
    }
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
  }

这就是全部,让我知道它是否适合你。