Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, I've a style for scrollviewer for my wpf application, The style goes in a Resource file named TH_ScrollViewer.xaml and has been added in application's ResourceDictionay

XML
<Application.Resources>
       <ResourceDictionary>
           <ResourceDictionary.MergedDictionaries>
               <ResourceDictionary Source="ThemeStyles\TH_ScrollViewer.xaml"/>
           </ResourceDictionary.MergedDictionaries>
       </ResourceDictionary>
   </Application.Resources>


Style goes here.

XML
<Style x:Key="{x:Type ScrollViewer}" TargetType="{x:Type ScrollViewer}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ScrollViewer}">
                    <Grid removed="{TemplateBinding Background}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>

                        <ScrollContentPresenter x:Name="PART_ScrollContentPresenter"
              KeyboardNavigation.DirectionalNavigation="Local"
              CanContentScroll="True" CanHorizontallyScroll="False"
              CanVerticallyScroll="False" Margin="2,1,2,1"/>

                        <ScrollBar x:Name="PART_HorizontalScrollBar"
              Orientation="Horizontal"
              Grid.Row="1"
              Maximum="{TemplateBinding ScrollableWidth}"
              ViewportSize="{TemplateBinding ViewportWidth}"
              Value="{TemplateBinding HorizontalOffset}"
              Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Template="{DynamicResource HorizontalScrollBar}"/>

                        <ScrollBar x:Name="PART_VerticalScrollBar"
              Grid.Column="1"
              Maximum="{TemplateBinding ScrollableHeight}"
              ViewportSize="{TemplateBinding ViewportHeight}"
              Value="{TemplateBinding VerticalOffset}"
              Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Template="{DynamicResource VerticalScrollBar}"/>

                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


What strange problem i'm facing is when i change x:key of this style and implement in control using Style and setting Dynamic Resource NewKeyVal, it gets the style but but default it doesn't gets it.

Kindly suggest.

Thanks,
ABhishek
Posted

from StackOverflow (http://stackoverflow.com/a/4166345/1218349):
Quote:
On the creation of every object in XAML, if a default style is present (i.e. style w/ a key of Type) that style should be applied. As you can imagine there are several performance optimizations to make that (implied) lookup a light weight as possible. One of them is that we don’t look inside Resource Dictionaries unless they are flagged as “containing default Styles”. There is a bug: if all your default styles are nested in merged dictionaries three levels deep (or deeper) the top dictionary does not get flagged so the search skips it. The work around is to put a default Style to something, anything, in the root Dictionary.

HTML
<application.resources>
       <resourcedictionary>
           <resourcedictionary.mergeddictionaries>
               <resourcedictionary>
                   Source="ThemeStyles\TH_ScrollViewer.xaml" />
               </resourcedictionary></resourcedictionary.mergeddictionaries>
           <!-- Dummy Style, anything you won't use goes -->
           <style targettype="{x:Type Rectangle}" />
       </resourcedictionary>
   </application.resources>

Maybe it's your case
 
Share this answer
 
it's by design. The implicit key for the style is TargetType. If it's the only key, the style will be used as default control style. But if you set x:Key, the style will be only applied when it is explicitly referenced.
So, if you need some style for all controls in application, remove x:Key from it.
 
Share this answer
 
Comments
Abhishek Kumar (here to help!!) 20-Nov-13 7:32am    
@Irina Pykhova : I tried but problem remains the same. I removed x:key from style and removed usage of Style at control calling, but still its not pulling up the style.
Irina Pykhova 20-Nov-13 8:52am    
check other parts of your project, maybe you have other styles for ScrollViewer which replace the default one.
Hi,
You would have missed the namespace path and the path what you have mentioned looks like a invalid path

XML
<resourcedictionary source="/<namespace>;component/ThemeStyles/TH_ScrollViewer.xaml" />


Hope this will help you.
 
Share this answer
 
v2
Comments
Abhishek Kumar (here to help!!) 20-Nov-13 7:51am    
Actually when i give x:key= someval and then call style of the respective control using that key val , it pulls up the style, so path of style is proper, never-the-less i tried your approach also but couldn't get the job done.
[no name] 20-Nov-13 22:18pm    
sorry about that. because i had a same problem but i tried this one and it works for me.
Try this
Sample Code:
HTML
<resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <resourcedictionary.mergeddictionaries>
        <resourcedictionary source="Windows8/Telerik.Windows.Controls.RibbonView.xaml" />
        <resourcedictionary>
            <style x:key="MyThemeRadRibbonViewStyle" targettype="{x:Type telerik:RadRibbonView}" basedon="{StaticResource ResourceKey=RadRibbonViewStyle}" xmlns:x="#unknown" />
        </resourcedictionary>
    </resourcedictionary.mergeddictionaries>
</resourcedictionary>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900