【问题标题】:Issue creating style to be set as the GroupStyle of some ComboBoxes问题创建样式设置为某些组合框的 GroupStyle
【发布时间】:2013-03-24 11:43:40
【问题描述】:

我正在尝试实现following code,不同之处在于我希望将其应用于一种样式,以便我可以为我喜欢的任何ComboBox 设置它(即我正在创建许多组合框由于特定的不可更改的要求,动态地从后面的代码中获取,并希望将GroupStyles 添加到每个代码中)。

我对@9​​87654324@ 和XAML 比较陌生,所以我想通过Style 这样做,并在ControlTemplate 中指定GroupStyles,然后将样式应用于各自的@987654329 @。这是我迄今为止尝试过的,但代码无法编译(主要是由于<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>

【问题讨论】:

    标签: c# .net wpf xaml controltemplate


    【解决方案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; }
    }
    

    【讨论】:

    • 感谢您的建议 - 我如何将它应用于我需要的每个 ComboBox?能否展示一些示例 C# 代码?
    • 对于每个组合框,您应该通过StaticResource 绑定设置其GroupStyleHeaderTemplate 属性。我要编辑我的代码...
    • 这可以通过后面的代码应用,还是只能通过 XAML 应用?当我试图通过后面的代码来做到这一点时。谢谢
    • 感谢您的编辑和新代码。但我不明白的是,我将如何为新的 GroupStyles 设置文本?
    • 下面链接后面的代码你看清楚了吗? jarloo.com/wpf-combobox-with-groupings
    【解决方案2】:

    GroupStyle属性在combobox上,所以你需要单独设置而不是在模板中-

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

    编辑

    好吧,您不能从Style 设置GroupStyle 属性,因为它没有任何关联的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);
    

    【讨论】:

    • 我无法做到这一点 - 它报告说 GroupStyle 不是 DependencyProperty。
    • 更具体地说:Error 3 The property "GroupStyle" is not a DependencyProperty. To be used in markup, non-attached properties must be exposed on the target type with an accessible instance property "GroupStyle". For attached properties, the declaring type must provide static "GetGroupStyle" and "SetGroupStyle" methods.
    • 好吧,GroupStyle 是一个 DP,但它不能通过 Style 设置,因为它没有任何设置器。但是,您可以通过代码进行设置。有关详细信息,请参阅此处的答案 - stackoverflow.com/questions/12022529/…
    • 如果我从后面的代码中设置它,我该如何设置 GroupStyle 的标题呢?似乎不能做一个简单的groupStyle.Header = "Text"
    • 你必须在后面的代码中创建DataTemplate。我已经编辑了答案,看看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多