如何为自定义控件设置GroupStyle

时间:2013-06-18 06:02:50

标签: wpf custom-controls resourcedictionary

我创建了一个自定义控件库项目,并执行了以下操作:

  1. 自定义控件源自ComboBox
  2. 主题文件夹
  3. 下添加资源词典文件rd.xaml
  4. 在rd.xaml文件中定义一些样式

    <Style x:Key="GroupComboBoxStyle" TargetType="{x:Type local:GroupComboBox}">
    
        <Setter Property="ItemContainerStyle" >
            <Setter.Value>
                <Style TargetType="{x:Type ComboBoxItem}">
                    <Setter Property="IsEnabled" Value="{Binding Available}"/>
                </Style>
            </Setter.Value>
        </Setter>
    
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:GroupComboBox}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
    
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    
    
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <WrapPanel IsItemsHost="True" Orientation="Horizontal" Width="150" Height="Auto" >
                        <!-- add scroll bar -->
                    </WrapPanel>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
    
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Text="{Binding Item}" Width="40"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    
    </Style>
    
    <CollectionViewSource x:Key="groupedData" Source="{Binding Items}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="Category"/>
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
    
    <Style x:Key="groupComboBoxItemStyle" TargetType="{x:Type ComboBoxItem}">
        <Setter Property="Width" Value="50" />
    </Style>
    
    <GroupStyle x:Key="groupStyle">
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                <Border BorderBrush="Black" BorderThickness="2">
                    <TextBlock Text="{Binding Name}" HorizontalAlignment="Stretch" Background="YellowGreen"/>
                </Border>
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
    </GroupStyle>
    

  5. 然后我想在运行时将组样式设置为我的自定义控件

  6. 但是无法找到groupstyle,如何从资源字典文件中获取它?

    public GroupComboBox()
                {
                    GroupStyle style = new GroupStyle();
                    // get the groupstyle
                    style.HeaderTemplate = (DataTemplate)this.FindResource("groupStyle");
                    this.GroupStyle.Add(style);
                }
    

1 个答案:

答案 0 :(得分:0)

WPF CustomControl应该是看起来。这意味着,代码应仅包含控件的逻辑,但与其外观,样式等无关。这应该都是使用在 Generic.xaml 中为您创建的样式完成的。

无论如何,希望在标题中有一个绿色背景是完全有效的......我建议在你的控件中为DefaultGroupStyle创建一个可绑定的依赖属性。我已经实现并测试了它,它可以解决问题:

控件GroupComboBox

public class GroupComboBox : ComboBox
{
    public static readonly DependencyProperty DefaultGroupStyleProperty =
        DependencyProperty.Register("DefaultGroupStyle", typeof (GroupStyle), typeof (GroupComboBox), new PropertyMetadata(default(GroupStyle), OnDefaultGroupStyleChanged));

    private static void OnDefaultGroupStyleChanged(DependencyObject s, DependencyPropertyChangedEventArgs a)
    {
        var c = (GroupComboBox) s;
        if (a.NewValue == null) return;

        if (c.GroupStyle.Count == 0)
            c.GroupStyle.Add((GroupStyle) a.NewValue);
    }

    public GroupStyle DefaultGroupStyle
    {
        get { return (GroupStyle) GetValue(DefaultGroupStyleProperty); }
        set { SetValue(DefaultGroupStyleProperty, value); }
    }

    static GroupComboBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(GroupComboBox), new FrameworkPropertyMetadata(typeof(GroupComboBox)));
    }
}

以及 Generic.xaml 中的样式(随意将样式移动到另一个文件但不要忘记将其合并到 Generic.xaml 。请注意我删除了ComboBox默认样式的键。它不会自动应用,否则......

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfCustomControlLibrary1">

    <GroupStyle x:Key="GroupStyle">
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                <Border BorderBrush="Black" BorderThickness="2">
                    <TextBlock Text="{Binding Name}" HorizontalAlignment="Stretch" Background="YellowGreen"/>
                </Border>
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
    </GroupStyle>

    <Style TargetType="{x:Type local:GroupComboBox}">

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:GroupComboBox}">
                    <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">

                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

        <Setter Property="DefaultGroupStyle" Value="{StaticResource GroupStyle}" />

    </Style>
</ResourceDictionary>

请告诉我这是否适合您,如果有任何不清楚的地方,请随时询问。