1

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

  1. 自定义控件来源于ComboBox
  2. 在Themes文件夹下添加资源字典文件 rd.xaml
  3. 在 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>
    

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

但是找不到groupstyle,如何从资源字典文件中获取呢?

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

1 回答 1

1

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

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

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

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

于 2013-06-18T07:30:38.087 回答