WPF某些样式未应用于DataTemplate控件

Mar*_*tin 30 wpf xaml styles datatemplate

我正在尝试学习WPF的一些内容,我对它的灵活性感到非常惊讶.

但是,我遇到了Styles和DataTemplates 的问题,这有点令人困惑.我已经定义了以下测试页,与周围风格等有点玩,发现Style在定义小号<Page.Resources>Border,并TextBlock没有在应用DataTemplate,但Style对于ProgressBar以完全相同的方式定义应用.

源代码(我只使用Kaxaml和XamlPadX来查看结果)

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

  <Page.Resources>

    <Style TargetType="{x:Type Border}">
      <Setter Property="Background" Value="SkyBlue"/>
      <Setter Property="BorderBrush" Value="Black"/>
      <Setter Property="BorderThickness" Value="2"/>
      <Setter Property="CornerRadius" Value="5"/>
    </Style>

    <Style TargetType="{x:Type TextBlock}">
      <Setter Property="FontWeight" Value="Bold"/>
    </Style>

    <Style TargetType="{x:Type ProgressBar}">
      <Setter Property="Height" Value="10"/>
      <Setter Property="Width" Value="100"/>
      <Setter Property="Foreground" Value="Red"/>
    </Style>

    <XmlDataProvider x:Key="TestData" XPath="/TestData">
      <x:XData>
        <TestData xmlns="">
          <TestElement>
            <Name>Item 1</Name>
            <Value>25</Value>
          </TestElement>
          <TestElement>
            <Name>Item 2</Name>
            <Value>50</Value>
          </TestElement>
        </TestData>
      </x:XData>
    </XmlDataProvider>

    <HierarchicalDataTemplate DataType="TestElement">
      <Border Height="45" Width="120" Margin="5,5">
        <StackPanel Orientation="Vertical" Margin="5,5" VerticalAlignment="Center" HorizontalAlignment="Center">
          <TextBlock HorizontalAlignment="Center" Text="{Binding XPath=Name}"/>
          <ProgressBar Value="{Binding XPath=Value}"/>
        </StackPanel>
      </Border>
    </HierarchicalDataTemplate>

  </Page.Resources>

  <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
    <StackPanel Orientation="Vertical" VerticalAlignment="Center">
      <Border Height="45" Width="120" Margin="5,5">
        <StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center">
          <TextBlock HorizontalAlignment="Center" Text="Item 1"/>
          <ProgressBar Value="25"/>
        </StackPanel>
      </Border>
      <Border Height="45" Width="120" Margin="5,5">
        <StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center">
          <TextBlock HorizontalAlignment="Center" Text="Item 2"/>
          <ProgressBar Value="50"/>
        </StackPanel>
      </Border>
    </StackPanel>
    <ListBox Margin="10,10"  Width="140" ItemsSource="{Binding Source={StaticResource TestData}, XPath=TestElement}"/>
  </StackPanel>
</Page>
Run Code Online (Sandbox Code Playgroud)

我怀疑它与默认样式等有关,但更令人费解的是为什么有些Style是应用而有些则没有.我无法在任何地方找到一个简单的解释,因此想问一下是否有人能够用lamens的术语解释这种行为,并可能链接到技术描述,即MSDN等.

在此先感谢您的支持!

Bri*_*hey 35

我发现了一个简单的解决方法.对于任何无法在数据模板封装边界外搜索的元素(即未进行隐式设置),您只需在数据模板中为该元素类型声明一个空样式,并使用BasedOn该样式的属性来查找正确的要应用的数据模板外的隐式样式.

在下面的示例中,TextBox能够在数据模板封装边界之外进行搜索(因为它继承自Control?),但TextBlock无法进行搜索,因此我声明了可以在数据模板外搜索的空样式.

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <DataTemplate.Resources>
            <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}" />
        </DataTemplate.Resources>
        <DockPanel>
            <TextBlock Text="{Binding Name}"  />
            <TextBox Text="{Binding Value}" />
        </DockPanel>
    </DataTemplate>
</ItemsControl.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)


Cod*_*ked 30

这实际上是设计的.不从Control派生的元素将不会获取隐式样式,除非它们位于应用程序资源中.

链接更详细地解释了这一点,或者您可以查看Connent 错误报告.

  • 这两个链接现在都死了。找到[第一个的反向链接](https://web.archive.org/web/20170201072114/http://www.11011.net/archives/000692.html)。如果您愿意,请将其添加到您的答案中。 (2认同)