如何为WPF中的特定控件覆盖样式 [英] How to override styles for specific controls in WPF

查看:587
本文介绍了如何为WPF中的特定控件覆盖样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我们正在为列表视图及其用户控件应用通用样式.此用户控件列表视图具有一个资源文件.

对我来说,此列表视图之一需要应用单独的样式,以便为控件设置特定的高度.如果我在公共列表视图中设置,那么它将影响我们使用列表视图的屏幕.我要针对特定​​屏幕的列表视图进行操作.

我们可以这样做吗?请提出一些建议.

Hi,

We are applying a common style for a listview its a user control. This usercontrol listview has one resource file.

For me one of this listview need to apply a separate style in order to set specific height for the control. If i set in the common listview then it will impact screen where we use the listview. I want to do for specific screen's listview.

Can we do this? please some suggestions

推荐答案

我相信您可以将TargetType用于ListView等控件类型,并且可以对特定控件使用x:Key. ;请参见下面的示例:

标记:
< Window x:Class =" StyleForSpecificControl.Window1"
    xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation " ;
    xmlns:x =" http://schemas.microsoft.com/winfx/2006/xaml "
   标题="Window1".高度="300".宽度="300">
< Window.Resources>
       < Style TargetType ="{x:Type ListView}">
< Setter Property =高度"值="50"/>
</Style>
       < Style x:Key ="listviewStyle">
< Setter Property ="Control.Height";值="100"/>
</Style>
    </Window.Resources>
    < StackPanel>
       < ListView>
          < ListViewItem Content ="Item1"/>
< ListViewItem Content ="Item2"/>
< ListViewItem Content ="Item3"/>
< ListViewItem Content ="Item4"/>
< ListViewItem Content ="Item5"/>
< ListViewItem Content ="Item6"/>
< ListViewItem Content ="Item7"/>
< ListViewItem Content ="Item8"/>
< ListViewItem Content ="Item9"/>
       </ListView>
       < ListView Style =" {StaticResource listviewStyle}">
< ListViewItem Content ="Item1"/>
< ListViewItem Content ="Item2"/>
< ListViewItem Content ="Item3"/>
< ListViewItem Content ="Item4"/>
< ListViewItem Content ="Item5"/>
< ListViewItem Content ="Item6"/>
< ListViewItem Content ="Item7"/>
< ListViewItem Content ="Item8"/>
< ListViewItem Content ="Item9"/>
       </ListView>
    </StackPanel>
</Window>

希望对您有所帮助.

I believe you could use TargetType for control type such as ListView, and you could x:Key for specific controls.  Please see example below:

Markup:
<Window x:Class="StyleForSpecificControl.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <Style TargetType="{x:Type ListView}">
            <Setter Property="Height" Value="50"/>
        </Style>
        <Style x:Key="listviewStyle">
            <Setter Property="Control.Height" Value="100"/>
        </Style>
    </Window.Resources>
    <StackPanel>
        <ListView>
            <ListViewItem Content="Item1"/>
            <ListViewItem Content="Item2"/>
            <ListViewItem Content="Item3"/>
            <ListViewItem Content="Item4"/>
            <ListViewItem Content="Item5"/>
            <ListViewItem Content="Item6"/>
            <ListViewItem Content="Item7"/>
            <ListViewItem Content="Item8"/>
            <ListViewItem Content="Item9"/>
        </ListView>
        <ListView Style="{StaticResource listviewStyle}">
            <ListViewItem Content="Item1"/>
            <ListViewItem Content="Item2"/>
            <ListViewItem Content="Item3"/>
            <ListViewItem Content="Item4"/>
            <ListViewItem Content="Item5"/>
            <ListViewItem Content="Item6"/>
            <ListViewItem Content="Item7"/>
            <ListViewItem Content="Item8"/>
            <ListViewItem Content="Item9"/>
        </ListView>
    </StackPanel>
</Window>

Hope it helps.


这篇关于如何为WPF中的特定控件覆盖样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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