Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styles overwriting in WPF

Tags:

styles

wpf

xaml

I'm using MahApps.Metro to achieve Metro UI in my app.

I have a listview and MahApps.Metro is changing style for it. MahApps styles for listview are here.

Loading of styles:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.AnimatedSingleRowTabControl.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
            <ResourceDictionary Source="pack://application:,,,/FineRSS;component/Resources/Icons.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

I need to keep track of selected listviewitems so I'm using the next approach:

<ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
                </Style>
</ListView.ItemContainerStyle>

But MahApps.Metro's style is overwritten to ListView's default.

What can I do to keep either styles and IsSelected binding?

like image 216
xZ6a33YaYEfmv Avatar asked May 07 '12 21:05

xZ6a33YaYEfmv


People also ask

What is style in WPF?

WPF - Styles - The .NET framework provides several strategies to personalize and customize the appearance of an application. ... Multiple properties can be added into a single style. Styles are used to give a uniform look or appearance to a set of controls.

What are WPF styles and triggers?

In this article I will introduce you into the concept of WPF Styles and Triggers. WPF Styles consist of Setters and Triggers that are supposed to change the Style and Behavior of a Control. From a technical point of view the purpose of Styles is to set Dependency Properties on a Control.

How to override control templates in WPF?

Overriding control templates in WPF requires you to completely replace the template. You may have wanted to change just the one aspect of the template but the result of that is Expression dumping a copy of the rest of the template so that it can be overridden.

What is the scope of styles in XAML?

The scope of styles are similar to any other resources. The resource lookup process first looks at local styles and if not found, it traverses to the parent element in the logical tree and so on. In the end, the resource lookup process looks for styles in the application and themes. The Style element in XAML represents a style.


1 Answers

I'm not positive I follow what you're trying to do, but would it make sense to make your Style be BasedOn the default one that is loaded?

Something like

<ListView.ItemContainerStyle> 
    <Style TargetType="{x:Type ListViewItem}" 
           BasedOn="{StaticResource {x:Type ListViewItem}}"> 
        <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/> 
    </Style> 
</ListView.ItemContainerStyle> 
like image 87
Tim Avatar answered Nov 15 '22 09:11

Tim