开发者

WPF : Keeping default style when inheriting from ListBoxItem

开发者 https://www.devze.com 2023-02-16 13:22 出处:网络
Hey Guys (and girls if any :) I needed a different list box selection policy than the one provided by default with WPF listboxes, i.e. beeing able to have an extended selection withou any modifier ke

Hey Guys (and girls if any :)

I needed a different list box selection policy than the one provided by default with WPF listboxes, i.e. beeing able to have an extended selection withou any modifier key.

No problem on that, here is what I did :

class EnhancedCcyPairListBox : ListBox
{

    protected override DependencyObject GetContainerForItemOverride()
    {
       开发者_开发百科 return new CcyPairListBoxItem();
    }

}

internal class CcyPairListBoxItem : ListBoxItem
{
    protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        IsSelected = !IsSelected;

        e.Handled = true;
    }
}

I can't tell if it's the best way to do, but it seems to work exactly as I expected.

Except that... by doing so, I have lost the defaults ListBoxItem style I had before. How can I tell to my derived classes to keep their default style ?

Thank you very much !


Add this above the EnhancedCcyPairListBox declaration

    [StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(CcyPairListBoxItem))]

Edit

Add this to the static constructor of CcyPairListBoxItem"

            DefaultStyleKeyProperty.OverrideMetadata(
            typeof(CcyPairListBoxItem), new FrameworkPropertyMetadata(typeof(CcyPairListBoxItem)));

and in Themes/Generic.xaml add

<Style TargetType="{x:Type CcyPairListBoxItem}"
       BasedOn="{StaticResource {x:Type ListBoxItem}}"/>


Because the WPF context of our application is quite special, I wanted to be sure that it was not an issue that was very specific.

So I took the above two classes and imported them in a brand new WPF test project. I created a Themes directory that registered a Resource dictionnary in which I put the Style as NVM mentionned earlier.

WPF : Keeping default style when inheriting from ListBoxItem

Content of Generic.xaml :

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/InheritedListBoxStyling;component/Themes/Styles/TempDic.xaml" />
    <!-- Reference here the Resource dictionnary used for your own component -->
</ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

Content of TempDic.xaml :

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

<Style TargetType="{x:Type InheritedListBoxStyling:CcyPairListBoxItem}"        
       BasedOn="{StaticResource {x:Type ListBoxItem}}"/>

</ResourceDictionary>

Content of Window1.xaml.cs

namespace InheritedListBoxStyling
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            DataContext = this;
            for (int i = 0; i < 50; i++)
            {
                source.Add("toto " + i);
            }

            l1.ItemsSource = source;
            l2.ItemsSource = source;
        }

        public ObservableCollection<string> source = new ObservableCollection<string>();
    }
}

Content of Window1.xaml :

<Window x:Class="InheritedListBoxStyling.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:InheritedListBoxStyling="clr-namespace:InheritedListBoxStyling" 
    Title="Window1" Height="300" Width="300">
    <Grid>

        <ListBox x:Name="l1" SelectionMode="Extended" 
             Margin="0,0,12,12"  Height="100" 
             HorizontalAlignment="Right" 
             VerticalAlignment="Bottom" Width="120" />
        <InheritedListBoxStyling:EnhancedCcyPairListBox 
            x:Name="l2" SelectionMode="Extended" 
            Height="100" Margin="12,12,0,0" 
            VerticalAlignment="Top" 
            HorizontalAlignment="Left" Width="120" />
    </Grid>
</Window>

And here the result :

WPF : Keeping default style when inheriting from ListBoxItem

=> the default styling is not applied and, as in my "real case" issue, the only kind of styling that seems to be applied is the gray selected item style.

Any idea about what's going on or what I'm doing wrong ?

0

精彩评论

暂无评论...
验证码 换一张
取 消