1

私は次のコードを実装しようとしていますが、これをスタイルに適用して、好きなものに設定できるようにしたいという違いがありますComboBox(つまり、コードの後ろから動的に多数のComboBoxを作成しています。特定の変更不可能な要件、およびそれらのそれぞれに追加したいGroupStyles)。

私はとに比較的慣れていないので、aを使用してこれを行い、WPFでを指定してから、それぞれのにスタイルを適用することを考えました。これは私がこれまでに試したことですが、コードはコンパイルされません(主にパーツが原因です)。XAMLStyleGroupStylesControlTemplateComboBoxes<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>
4

2 に答える 2

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 に答える
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 に答える