1

I'm trying to implement the following code, with the difference that I would like this to apply to a style so that I can set it for any ComboBox that I like (i.e. I am creating a number of ComboBoxes dynamically from the code behind due to specific unchangeable requirements, and would like to add GroupStyles to each of them).

I am relatively new to WPF and XAML, so I thought of doing so through a Style and specifying the GroupStyles in the ControlTemplate, and then just applying the style to the respective ComboBoxes. This is what I've tried so far, but the code will not compile (mainly due to the <ComboBox.GroupStyle> part).

<Style x:Name="valuesComboStyle" TargetType="ComboBox">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <ComboBox.GroupStyle>
                                <GroupStyle>
                                    <GroupStyle.HeaderTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding Name}"/>
                                        </DataTemplate>
                                    </GroupStyle.HeaderTemplate>
                                </GroupStyle>
                            </ComboBox.GroupStyle>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
4

2 回答 2

1

定义DataTemple资源中的某处。Combobox并根据您的需要使用它。

这是代码:

<Grid>
    <Grid.Resources>
        <DataTemplate x:Key="groupStyle">
            <TextBlock FontWeight="Bold" Text="{Binding Name}"/>
        </DataTemplate>
        <Style TargetType="{x:Type ComboBoxItem}"  x:Key="comboBoxItemStyle">
            <Setter Property="Template" >
                <Setter.Value>
                    <ControlTemplate>
                        <Label Background="Red" Content="{Binding Item}"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>
    <ComboBox Height="27" Width="195" DisplayMemberPath="Item" Name="cboGroup"
              ItemContainerStyle="{StaticResource comboBoxItemStyle}">
        <ComboBox.GroupStyle>
            <GroupStyle HeaderTemplate="{StaticResource groupStyle}"/>
        </ComboBox.GroupStyle>
    </ComboBox>
</Grid>

编辑:我创建了一个新的组合框并设置了一些项目并设置了您正在寻找的样式。(我更新了您链接中的代码)

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;


        ComboBox comboBox1 = new ComboBox();
        comboBox1.Height = 23;
        comboBox1.Width = 200;

        GroupStyle style = new GroupStyle();
        style.HeaderTemplate = (DataTemplate)this.FindResource("groupStyle");
        comboBox1.GroupStyle.Add(style);
        comboBox1.DisplayMemberPath = "Item";
        ObservableCollection<CategoryItem<string>> items = new ObservableCollection<CategoryItem<string>>();

        items.Add(new CategoryItem<string> { Category = "Warm Colors", Item = "Orange" });
        items.Add(new CategoryItem<string> { Category = "Warm Colors", Item = "Red" });
        items.Add(new CategoryItem<string> { Category = "Warm Colors", Item = "Pink" });
        items.Add(new CategoryItem<string> { Category = "Cool Colors", Item = "Blue" });
        items.Add(new CategoryItem<string> { Category = "Cool Colors", Item = "Purple" });
        items.Add(new CategoryItem<string> { Category = "Cool Colors", Item = "Green" });

        CollectionViewSource cvs = new CollectionViewSource();
        cvs.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
        cvs.Source = items;

        Binding b = new Binding();
        b.Source = cvs;
        BindingOperations.SetBinding(
           comboBox1, ComboBox.ItemsSourceProperty, b);

        myGrid.Children.Add(comboBox1);
    }
}

public class CategoryItem<T>
{
    public T Item { get; set; }
    public string Category { get; set; }
}
于 2013-03-24T12:47:39.807 回答
1

GroupStyle属性位于组合框上,因此您需要单独设置它而不是在模板中 -

  <Style TargetType="ComboBox">
            <Setter Property="GroupStyle">
                <Setter.Value>
                    <GroupStyle>
                        <GroupStyle.HeaderTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Name}"/>
                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                    </GroupStyle>
                </Setter.Value>
            </Setter>
        </Style>

编辑

好吧,您无法设置该GroupStyle属性,Style因为它没有任何关联的设置器。

但是,您可以使用此处Add()说明的方法从代码隐藏中添加它,或者您必须创建自定义说明此处Attached property

背后的代码 -

        GroupStyle g = new GroupStyle();

        //Create header template
        FrameworkElementFactory control = new
                                  FrameworkElementFactory(typeof(TextBlock));
        Binding binding = new Binding();
        control.SetBinding(TextBlock.TextProperty, binding);
        binding.Path = new PropertyPath("Name");
        DataTemplate dataTemplate = new DataTemplate();
        dataTemplate.VisualTree = control;

        g.HeaderTemplate = dataTemplate;
        ComboBox cmb = new ComboBox();
        cmb.GroupStyle.Add(g);
于 2013-03-24T11:59:44.343 回答