WPF控件没有提取app.xaml中注册的默认样式 [英] WPF Controls not pulling up default style registered in app.xaml

查看:125
本文介绍了WPF控件没有提取app.xaml中注册的默认样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我为我的wpf应用程序提供了一个scrollviewer样式,该样式位于一个名为TH_ScrollViewer.xaml的资源文件中,并已添加到应用程序的ResourceDictionay中



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

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





样式就在这里。





Style goes here.

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





我面临的一个奇怪的问题是当我改变x:这个样式的键并使用Style和设置动态资源NewKeyVal实现控制时,它获得了样式但是默认它没有得到它。



请建议。



谢谢,

ABhishek



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

推荐答案

http://stackoverflow.com/a/4166345/ 1218349 ):

from StackOverflow (http://stackoverflow.com/a/4166345/1218349):
Quote:

关于在XAML中创建每个对象,如果是默认样式存在(即具有Type的键的样式)应该应用该样式。您可以想象,有几种性能优化可以使(隐含)查找尽可能轻量级。其中之一是我们不会查看资源字典,除非它们被标记为包含默认样式。有一个错误:如果所有默认样式都嵌套在合并的字典中三个级别(或更深),则顶级字典不会被标记,因此搜索会跳过它。解决方法是在根词典中将默认样式放到某个东西,任何东西。

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.




<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


它是设计的。样式的隐式键是 TargetType 。如果它是唯一的键,则样式将用作默认控件样式。但是如果你设置 x:Key ,那么只有在显式引用时才会应用样式。

所以,如果你需要一些风格的话应用程序中的控件,从中删除 x:Key
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.




你会错过命名空间路径和你提到的路径看起来像一个无效的路径



Hi,
You would have missed the namespace path and the path what you have mentioned looks like a invalid path

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





希望这对你有帮助。



Hope this will help you.


这篇关于WPF控件没有提取app.xaml中注册的默认样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆