问题创建样式要设置为某些ComboBoxes的GroupStyle

时间:2013-03-24 11:43:41

标签: c# .net wpf xaml controltemplate

我正在尝试实现following code,区别在于我希望将其应用于样式,以便我可以为任何我喜欢的ComboBox设置它(即我正在创建由于特定的不可更改的要求,后面的代码动态地动态生成了许多ComboBox,并希望为每个组件添加GroupStyles

我对WPFXAML相对较新,所以我想通过Style并在GroupStyles中指定ControlTemplate来实现这一目标,然后将样式应用于相应的ComboBoxes。这是我到目前为止所尝试的,但代码不会编译(主要是由于<ComboBox.GroupStyle>部分)。

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

2 个答案:

答案 0 :(得分: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属性,因为它没有与之关联的任何setter。

然而,您可以使用Add()方法从代码隐藏中添加它,例如解释here,或者您必须创建自定义Attached property解释here

代码背后 -

        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);

答案 1 :(得分: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; }
}