全局风格不工作在WPF [英] Global Style not working in WPF

查看:82
本文介绍了全局风格不工作在WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么IST这个风格WPF不工作?将TextBlock应该是红色,但事实并非如此。它保持黑色。当TextBlock的是一个模板这只是happing

 <窗口x:类=WpfApplication2.MainWindow
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
标题=主窗口HEIGHT =350WIDTH =525>
< Window.Resources>
<风格的TargetType =TextBlock的>
< setter属性=前景VALUE =红>< /二传手>
< /样式和GT;
< /Window.Resources>
<网格和GT;
<&的ListView GT;
< ListView.Items>
<&列表项GT;< /列表项>
< /ListView.Items>
< ListView.ItemTemplate>
<&DataTemplate的GT;
<&的TextBlock GT;喂< / TextBlock的>
< / DataTemplate中>
< /ListView.ItemTemplate>
< /&的ListView GT;

< /网格和GT;
< /窗GT;


解决方案

样式模板是有限的,从 System.Windows.Controls.Control 继承,除非它们在 Application.Resources 所以要么给你的风格 X:键并明确使用它:

 < Window.Resources> 
<风格的TargetType =TextBlock的X:键=myTextBlockStyle>
< setter属性=前景VALUE =红>< /二传手>
< /样式和GT;
< /Window.Resources>

< TextBlock的风格={StaticResource的myTextBlockStyle}>喂< / TextBlock的>

或将其移动到 Application.Resources

 < Application.Resources> 
<风格的TargetType =TextBlock的>
< setter属性=前景VALUE =红>< /二传手>
< /样式和GT;
< /Application.Resources>


Why ist this Style not working in WPF? The TextBlock should be red, but it is not. It stays black. This is just happing when the TextBlock is in a Template.

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Foreground" Value="Red"></Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <ListView>
            <ListView.Items>
                <ListItem></ListItem>
            </ListView.Items>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock>Hallo</TextBlock>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

    </Grid>
</Window>

解决方案

Implicit Styles in templates are limited to controls that inherit from System.Windows.Controls.Control unless they are defined in Application.Resources so either give your style x:Key and use it explicitly:

<Window.Resources>
    <Style TargetType="TextBlock" x:Key="myTextBlockStyle">
        <Setter Property="Foreground" Value="Red"></Setter>
    </Style>
</Window.Resources>

<TextBlock Style="{StaticResource myTextBlockStyle}">Hallo</TextBlock>

or move it to Application.Resources

<Application.Resources>
    <Style TargetType="TextBlock">
        <Setter Property="Foreground" Value="Red"></Setter>
    </Style>
</Application.Resources>

这篇关于全局风格不工作在WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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