Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XAML扩展ListBox和ListBoxItem控件_C#_Wpf_Xaml - Fatal编程技术网

C# XAML扩展ListBox和ListBoxItem控件

C# XAML扩展ListBox和ListBoxItem控件,c#,wpf,xaml,C#,Wpf,Xaml,我正在尝试使用控件中定义的资源字典设置控件的默认样式,以便在整个应用程序中向样式添加事件处理程序,以根据绑定更改背景颜色。当前,将事件处理程序添加到底部代码段中显示的样式时,该样式将被覆盖 我对控件的外观很满意,因此我从样式中删除了代码,使其更具可读性 问题是在向bubblelist添加触发器时,默认样式会被覆盖,如上一个代码段所示 控件后面的代码: public partial class BubbleList : ListBox { public BubbleList() {

我正在尝试使用控件中定义的资源字典设置控件的默认样式,以便在整个应用程序中向样式添加事件处理程序,以根据绑定更改背景颜色。当前,将事件处理程序添加到底部代码段中显示的样式时,该样式将被覆盖

我对控件的外观很满意,因此我从样式中删除了代码,使其更具可读性

问题是在向bubblelist添加触发器时,默认样式会被覆盖,如上一个代码段所示

控件后面的代码:

public partial class BubbleList : ListBox
{
    public BubbleList()
    {
        InitializeComponent();
    }

    static BubbleList()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(BubbleList),
            new FrameworkPropertyMetadata(typeof(BubbleList)));
    }
}
<ListBox ... >

    <ListBox.Resources>
        <Style TargetType="{x:Type local:BubbleList}"
               BasedOn="{StaticResource {x:Type ListBox}}">    
            ....
        </Style>
    </ListBox.Resources>
</ListBox>
<lists:BubbleList ...>
    <lists:BubbleList.Style>
        <Style TargetType="lists:BubbleList"
               BasedOn="{StaticResource {x:Type lists:BubbleList}}">
            ...
        </Style>
    </lists:BubbleList.Style>
</lists:BubbleList ...>
从这里可以找到:

控件的样式设置方式:

public partial class BubbleList : ListBox
{
    public BubbleList()
    {
        InitializeComponent();
    }

    static BubbleList()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(BubbleList),
            new FrameworkPropertyMetadata(typeof(BubbleList)));
    }
}
<ListBox ... >

    <ListBox.Resources>
        <Style TargetType="{x:Type local:BubbleList}"
               BasedOn="{StaticResource {x:Type ListBox}}">    
            ....
        </Style>
    </ListBox.Resources>
</ListBox>
<lists:BubbleList ...>
    <lists:BubbleList.Style>
        <Style TargetType="lists:BubbleList"
               BasedOn="{StaticResource {x:Type lists:BubbleList}}">
            ...
        </Style>
    </lists:BubbleList.Style>
</lists:BubbleList ...>
已删除依赖项的BubbleListItem

public class BubbleListItem : ListBoxItem
{
    ...

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

    ...
}
liststyles.xaml中的两种样式:

<ResourceDictionary ... >
    <Style TargetType="{x:Type local:BubbleListItem}"
           BasedOn="{StaticResource {x:Type ListBoxItem}}">
        ...
    </Style>

    <Style TargetType="{x:Type local:BubbleList}"
           BasedOn="{StaticResource {x:Type ListBox}}">
        ...
    </Style>
</ResourceDictionary>
Themes/generic.xaml

<ResourceDictionary ... >
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="liststyles.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
用法:

<lists:BubbleList ... >
    <lists:BubbleList.ItemContainerStyle>
        <Style TargetType="{x:Type lists:BubbleListItem}"
               BasedOn="{StaticResource {x:Type lists:BubbleListItem}}">
            <Style.Triggers>
                ...
            </Style.Triggers>
        </Style>

    </lists:BubbleList.ItemContainerStyle>
    <lists:BubbleList.ItemTemplate>
        <DataTemplate>
            <Label ... />
        </DataTemplate>
    </lists:BubbleList.ItemTemplate>
</lists:BubbleList>

虽然控件的默认样式位于Themes/Generic.xaml中的特殊ResourceDictionary中,但在使用该控件时,可以轻松地对其进行扩展:

public partial class BubbleList : ListBox
{
    public BubbleList()
    {
        InitializeComponent();
    }

    static BubbleList()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(BubbleList),
            new FrameworkPropertyMetadata(typeof(BubbleList)));
    }
}
<ListBox ... >

    <ListBox.Resources>
        <Style TargetType="{x:Type local:BubbleList}"
               BasedOn="{StaticResource {x:Type ListBox}}">    
            ....
        </Style>
    </ListBox.Resources>
</ListBox>
<lists:BubbleList ...>
    <lists:BubbleList.Style>
        <Style TargetType="lists:BubbleList"
               BasedOn="{StaticResource {x:Type lists:BubbleList}}">
            ...
        </Style>
    </lists:BubbleList.Style>
</lists:BubbleList ...>

我假设有一种方法可以从默认样式覆盖中指定资源的位置。我想不是,它应该在Themes/Generic.xaml中。尽管有人告诉我不要使用ResourceDictionary Themes/Generic.xaml,但这似乎是完成所需操作的唯一方法。我将用完整的解决方案更新我的问题,以帮助他人。感谢您的回答: