如果TargetType被設定為ListBox,它可以作業。 但我想在ListBox內將TargetType設定為TextBlock。 我知道我可以使用x:Key,但為什么不給x:Key就不作業呢?
<Window x:Class="WPFApp6.DataTemplatesLab"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"/span>
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"/span>
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"/span>
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"/span>
xmlns:local="clr-namespace:WpfApp6"/span>
mc:Ignorable="d"。
Title="DataTemplatesLab"/span> Height="450"/span> Width="800"/span>>
<Window.Resources>
<SolidColorBrush x:Key="DataColor"/span> Color="Firebrick"/span>/>
<Style TargetType="TextBlock">
<Setter Property="FontSize"Value="28"/>
<Setter Property="Background" Value="Cyan"/>
</Style>
</Window.Resources>
<ListBox x:Name="ListPersonals" Width="600">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*"/span>/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Name}" Grid.Row="0" />
<TextBlock Text="{Binding Gender}" Grid.Row="1" />
<TextBlock Text="{Binding Email}" Grid.Row="2" />
<TextBlock Text="{Binding Title}" Grid.Row="3" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Window>
uj5u.com熱心網友回復:
你將需要使用鍵或者在DataTemplate級別定義樣式。
<ListBox x:Name="ListPersonals" Width="600" ItemsSource="{Binding DemoCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<DataTemplate.Resources>
<Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}" />
</DataTemplate.Resources>
這是因為DataTemplate作為一個封裝邊界作業。這意味著任何基于TargetType的樣式查詢都會在DataTemplate上停止。
請注意App.xaml中定義的樣式是這一規則的例外。如果你在app.xaml中定義樣式資源,即使不在DataTemplate中重新定義,該樣式也將應用于內部控制元件
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/324489.html
標籤:
上一篇:棱鏡。通過活動結束對話
