我想了解一些关于WPF的内容,我很惊讶它的灵活性。WPF一些样式不适用于DataTemplate控件
但是,我遇到了Style
s和DataTemplate
s的问题,这有点令人困惑。 我在下面的测试页定义为与周围风格等有点玩,发现<Page.Resources>
定义的Style
S代表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>
我怀疑它是与默认样式等,但更令人费解的是,为什么被应用于一些Style
S和一些不。我无法在上面找到一个简单的解释,因此想问一下,如果有人愿意用拉曼的话来解释这种行为,并且可能与技术描述(即MSDN)等有关。
在此先感谢您的支持!
感谢您的链接! 它解释了发生了什么,但仍然链接到官方文档将很好,只是我知道在哪里寻找这样的事情。 – Martin 2010-03-20 06:34:04
添加链接到Microsoft的Connect站点上的错误报告,不确定是否有任何官方文档描述此行为。 – CodeNaked 2010-03-24 15:37:08
现在这两个链接都死了。找到[第一个wayback链接](https://web.archive.org/web/20170201072114/http://www.11011.net/archives/000692.html)。如果你喜欢,请将它添加到你的答案中。 – itsho 2017-09-27 07:25:37