你怎么能重复使用整个WPF应用程序的样式? [英] How can you reuse a style throughout a WPF application?

查看:128
本文介绍了你怎么能重复使用整个WPF应用程序的样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用下面的代码段(从发现 http://stackoverflow.com/a/3675110/782880)在几个地方在我的应用程序。相反,复制/粘贴无处不在的,我怎么可以把这个在一个地方,并引用它的具体列表框(按键?)在不同的XAML文件?

 <列表框....>
    < ListBox.Resources>
            <风格的TargetType =ListBoxItem的>
                < setter属性=模板>
                    < Setter.Value>
                        <的ControlTemplate的TargetType =ListBoxItem的>
                            < BORDER名称=边境填充=2SnapsToDevicePixels =真正的>
                                <内容presenter />
                            < /边框>
                            < ControlTemplate.Triggers>
                                <触发属性=IsSelectedVALUE =真正的>
                                    <二传手的TargetName =边框属性=背景
                                            值={DynamicResource {X:静态SystemColors.HighlightBrushKey}}/>
                                < /触发>
                            < /ControlTemplate.Triggers>
                        < /控件模板>
                    < /Setter.Value>
                < /二传手>
        < /样式和GT;
    < /ListBox.Resources>
< /列表框>
 

解决方案

您可以把它变成一个资源集合在适当的水平。例如,如果你想要的应用范围,然后将其放在 App.xaml中

例如。

 <应用
  的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
  的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
  的StartupUri =MainWindow.xaml
  >

 < Application.Resources>
   <风格的TargetType =ListBoxItem的>
     < setter属性=模板>
       ...
     < /二传手>
   < /样式和GT;
 < /Application.Resources>

< /用途>
 

您可以给你的资源键,然后用相应的键,例如设置相应的样式属性与主要定义你的风格:

 <风格X:关键=myStyle的的TargetType =ListBoxItem的>
 

和者皆使用资源:

 < ListBox的X:名称=lstItemsItemContainerStyle ={的StaticResource myStyle的}>
 

I'd like to use the snippet below (found from http://stackoverflow.com/a/3675110/782880) in several places in my application. Instead of copy/pasting everywhere, how can I put this in one place and reference it for specific listboxes (by key?) in various XAML files?

<ListBox....> 
    <ListBox.Resources> 
            <Style TargetType="ListBoxItem"> 
                <Setter Property="Template"> 
                    <Setter.Value> 
                        <ControlTemplate TargetType="ListBoxItem"> 
                            <Border Name="Border" Padding="2" SnapsToDevicePixels="true"> 
                                <ContentPresenter /> 
                            </Border> 
                            <ControlTemplate.Triggers> 
                                <Trigger Property="IsSelected" Value="true"> 
                                    <Setter TargetName="Border" Property="Background" 
                                            Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> 
                                </Trigger> 
                            </ControlTemplate.Triggers> 
                        </ControlTemplate> 
                    </Setter.Value> 
                </Setter> 
        </Style> 
    </ListBox.Resources> 
</ListBox> 

解决方案

You can place it into a Resources collection at the appropriate level. For example, if you want application scope, then place it in App.xaml.

E.g.

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

 <Application.Resources>
   <Style TargetType="ListBoxItem"> 
     <Setter Property="Template"> 
       ...                      
     </Setter>
   </Style> 
 </Application.Resources>

</Application>

You can give your resources keys, and then set the appropriate Style property using the appropriate key, e.g. define your style with key:

<Style x:Key="MyStyle" TargetType="ListBoxItem">

and use the resource by key:

<ListBox x:Name="lstItems" ItemContainerStyle="{StaticResource MyStyle}">

这篇关于你怎么能重复使用整个WPF应用程序的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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