WPF SubControl (e.g. TextBlock) does not inherit style from the window using TemplateSelector

I need help because I don’t understand why the controls coming from the data table do not inherit the style defined in the window resources. Maybe there is a workaround?

I would be very grateful if someone could give me a solution, because I spent a lot of time trying to find something.

Hereby is my example. For example, the Texblock in the horrizontal Template does not align:

Udapte: I added background colors. The style applies to the label, but not to totextblock and the text field defined using the data template.

<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);
        }
    }
}
+4
source share
2 answers

, , , - , TextBlock ( DataTemplate, ControlTemplate). , Label, TextBox ( , TextBox, , ).

, - TextBlock :

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

, , , TextBlocks (DataTemplate ControlTemplate).

, , , , .

+3

, , TextBlock , WPF , , Control; , Control, .

, , FrameworkElement:

// FindImplicitSytle(fe) : Default: unlinkedParent, deferReference
internal static object FindImplicitStyleResource(
    FrameworkElement fe,
    object resourceKey,
    out object source)
{
    ...

    // For non-controls the implicit StyleResource lookup must stop at
    // the templated parent. Look at task 25606 for further details.
    DependencyObject boundaryElement = null;
    if (!(fe is Control))
    {
        boundaryElement = fe.TemplatedParent;
    }

    ...
}

Microsoft :

, , , , , , , , . . :

<StackPanel>
  <StackPanel.Resources> 
    <Style TargetType="TextBlock"> 
      <Setter Property="FontSize" Value="16"/> 
      <Setter Property="Foreground" Value="Green"/> 
    </Style>
  </StackPanel.Resources>

  <TextBlock HorizontalAlignment="Center" Text="Hello!"/> 
  <Button Content="Click me!" Width="200"/> 
  <TextBlock HorizontalAlignment="Center" Text="Please click the button"/>
</StackPanel>

, TextBlock TextBlock. TextBlock Button , , XAML :

Example Image

, , . , , , RepeatButtons . RepeatButton , RepeatButtons , RepeatButton ControlTemplate.

+6

All Articles