WPF子控件(如TextBlock)不会从带有TemplateSelector的窗口继承样式 [英] WPF SubControl (like TextBlock) doesn't inherit Style from window with TemplateSelector

查看:59
本文介绍了WPF子控件(如TextBlock)不会从带有TemplateSelector的窗口继承样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助,因为我不理解为什么来自数据模板的控件不继承窗口资源中定义的样式.可能有解决方法吗?

I need help because I don't understand why the controls coming from a datatemplate doesn't inherit the style defined in the window resources. May be is there a workaround?

如果有人能给我解决方案,我将非常感激,因为我花了很多时间来寻找一些东西.

在这里,我的前妻.例如,水平模板中的Texblock不对齐:

Hereby my exmaple. For instance the Texblock in horrizontal Template is not align:

Udapte:我添加了背景色.该样式将应用于标签,而不应用于数据模板定义的文本块和文本框.

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:localview="clr-namespace:WpfApplication3"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <Style x:Key="{x:Type TextBlock}" TargetType="TextBlock" >
            <Setter Property="Background" Value="Cyan"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Margin" Value="3"/>
            <Setter Property="FontFamily" Value="Comic Sans MS"/>
        </Style>
        <Style x:Key="{x:Type Label}" TargetType="Label">
            <Setter Property="Background" Value="Red"/>
            <Setter Property="VerticalAlignment" Value="Center" />
        </Style>
        <Style x:Key="{x:Type TextBox}" TargetType="TextBox">
            <Setter Property="Background" Value="Cyan"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Margin" Value="3"/>
        </Style>
        <Style x:Key="{x:Type ComboBox}" TargetType="ComboBox">
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Margin" Value="3"/>
        </Style>

        <localview:TemplateSelector x:Key="TemplateSelector">
            <localview:TemplateSelector.DataTemplateH>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="Value"/>
                        <TextBox Text="{Binding Path=SelectedItem.Content ,ElementName=Combo}"/>
                    </StackPanel>
                </DataTemplate>
            </localview:TemplateSelector.DataTemplateH>
            <localview:TemplateSelector.DataTemplateV>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <Label Content="Value"/>
                        <StackPanel Orientation="Horizontal">
                            <Label Content="new line"/>
                            **<TextBlock Text="{Binding Path=SelectedItem.Content ,ElementName=Combo}" TextAlignment="Right"/>**
                        </StackPanel>
                    </StackPanel>
                    </DataTemplate>
            </localview:TemplateSelector.DataTemplateV>
        </localview:TemplateSelector>

    </Window.Resources>


    <StackPanel Orientation="Vertical">

        <StackPanel>
            <TextBlock Text="Texblock"/>
            <TextBox Text="Texblock"/>
            <StackPanel Orientation="Horizontal">
                <Label Content="Value"/>
                <ComboBox Name="Combo">
                    <ComboBox.Items>
                        <ComboBoxItem Content="H"/>
                        <ComboBoxItem Content="V"/>
                    </ComboBox.Items>
                </ComboBox>
            </StackPanel>
            <ContentControl  ContentTemplateSelector="{StaticResource TemplateSelector}" 
                                      Content="{Binding Path=SelectedItem.Content ,ElementName=Combo}" />
        </StackPanel>

    </StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Reflection;


namespace WpfApplication3
{
    public class TemplateSelector : DataTemplateSelector
    {

        public DataTemplate DataTemplateH
        {
            get;
            set;
        }

        public DataTemplate DataTemplateV
        {
            get;
            set;
        }


        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            string s = (string)item;

            if (s == "H")
                return DataTemplateH;

            if (s == "V")
                return DataTemplateV;

            return base.SelectTemplate(item, container);
        }
    }
}

推荐答案

我刚刚尝试了一些简单的演示,是的,答案是您不能将模板外部定义的默认样式应用于内部的TextBlock (包括DataTemplate和ControlTemplate).其他控件(例如Label,TextBox)不会发生这种情况(尽管您也说过Style不适用于TextBox,但我尝试过,但实际上并非如此).

I've just tried some simple demos and yes the answer is you cannot apply default Style defined somewhere outside the template to some TextBlock inside the template (including both DataTemplate and ControlTemplate). This does not happen to other controls such as Label, TextBox (although you also said the Style did not apply on the TextBox, but I tried it and actually that's not true).

要解决此问题,最好的方法是为TextBlock明确设置样式,如下所示:

To fix the issue, the best way is set the Style explicitly for the TextBlock something like this:

<TextBlock Text="{Binding Path=SelectedItem.Content ,ElementName=Combo}" 
           TextAlignment="Right" Style="{StaticResource {x:Type TextBlock}}"/>

请注意,正如我所说,只有TextBlocks 内部 模板(DataTemplate和ControlTemplate)才需要.

Note that as I said, it's required only for TextBlocks inside template (DataTemplate and ControlTemplate).

该代码看起来相当荒谬,但实际上可以正常工作,而您并没有那样做,就像您看到的那样.

The code looks fairly ridiculous but it actually works, without doing that way as you see it won't work.

这篇关于WPF子控件(如TextBlock)不会从带有TemplateSelector的窗口继承样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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