Why are resources not used in this page?

自闭症网瘾萝莉.ら 提交于 2020-12-15 06:25:09

问题


No matter where I put my resources, their value isn't used:

enter image description here


The TextBlock text should be rendered black, but it's always rendered white.

Why?


Here is the source:

<Page
  x:Class="TemplateTest.MainPage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="d" Background="Lavender">

  <Page.Resources>
    <Style TargetType="TextBlock">
      <Setter Property="Foreground" Value="Black" />
    </Style>
  </Page.Resources>

  <StackPanel>
    <ListView>
      <ListView.HeaderTemplate>
        <DataTemplate>
          <TextBlock>
            <Run>Test</Run>
            <Run>!</Run>
          </TextBlock>
        </DataTemplate>
      </ListView.HeaderTemplate>

      <ListView.ItemTemplate>
        <DataTemplate x:DataType="x:String">
          <TextBlock Text="{x:Bind}"></TextBlock>
        </DataTemplate>
      </ListView.ItemTemplate>

      <x:String>Number 1</x:String>
      <x:String>Number 2</x:String>
      <x:String>Number 3</x:String>
    </ListView>
  </StackPanel>
</Page>

回答1:


Use x:key and provide a name to it and then refrence that name using staticresource in textblock:

<Page
  x:Class="TemplateTest.MainPage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="d" Background="Lavender">

    <Page.Resources>
        <Style TargetType="TextBlock" x:Key="tbstyle">
          <Setter Property="Foreground" Value="Black" />
        </Style>
    </Page.Resources>

    <StackPanel>
        <ListView>
          <ListView.HeaderTemplate>
            <DataTemplate>
              <TextBlock Style={StaticResources tbstyle}>
                <Run>Test</Run>
                <Run>!</Run>
              </TextBlock>
            </DataTemplate>
          </ListView.HeaderTemplate>

          <ListView.ItemTemplate>
            <DataTemplate x:DataType="x:String">
              <TextBlock Text="{x:Bind}" Style={StaticResources tbstyle}></TextBlock>
            </DataTemplate>
          </ListView.ItemTemplate>

          <x:String>Number 1</x:String>
          <x:String>Number 2</x:String>
          <x:String>Number 3</x:String>
        </ListView>
    </StackPanel>
</Page>



回答2:


From Jim Walker @ https://github.com/MicrosoftDocs/windows-uwp/issues/2790#issuecomment-722065687 :

TextBlock is different from most controls in that it doesn't have an editable template, so, unfortunately, it doesn't support lightweight styling. You have to use a Style to set the foreground. And, as we already know, a Page- or App-level implicit style won't be applied to TextBlocks inside a DataTemplate, so you have to use a separate implicit Style inside the DataTemplate or a Style with a key to change those TextBlocks . As far as I know, there isn't any way around this.



来源:https://stackoverflow.com/questions/64637614/why-are-resources-not-used-in-this-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!