[英]WPF: Modifying a ControlTemplate property from a DataTemplate?
使用MVVM方法來實現WPF,我有一個名為SubButton
的視圖模型類。 它的樣式如下:
<!-- SubNavButton Appearance -->
<ControlTemplate x:Key="SubNavButton" TargetType="{x:Type RadioButton}">
<Grid Margin="5,5">
<Rectangle x:Name="rectBackground" Fill="DimGray" Stroke="#FF000000" Width="150" Height="40" StrokeThickness="1"/>
<TextBlock x:Name="txtContent" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Arial" FontSize="16">
<ContentPresenter />
</TextBlock>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fill" TargetName="rectBackground" Value="Red"/>
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Fill" TargetName="rectBackground" Value="Pink"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<!-- SubButton data template -->
<DataTemplate DataType="{x:Type VM:SubButton}">
<RadioButton
Template="{StaticResource SubNavButton}"
Content="{Binding TempText}"
Command="{Binding SubFrameChanger.Command}"
CommandParameter="{Binding Key}"
GroupName="{Binding MenuGroup}"
V:CreateCommandBinding.Command="{Binding SubFrameChanger}" />
<DataTemplate.Triggers>
<!-- This trigger doesn't work -->
<DataTrigger Binding="{Binding Path=Selected}" Value="True">
<Setter TargetName="rectBackground" Property="Fill" Value="Green"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
DataTrigger
不起作用。 Selected
是SubButton
類上的常規.Net屬性。 我收到一個編譯錯誤,因為編譯器無法確定rectBackground
目標的來源。 它是ControlTemplate
一部分,我不確定如何告訴它? 關於DataContext
東西?
您想要的是不可能的。 WPF與NameScopes一起使用,名稱rectBackground在DataTemplate中超出范圍。 原始名稱rectBackground將僅在原始ControlTemplate的范圍內。 這很幸運,因為否則您將無法在整個應用程序中使用重復的名稱。 您可以做的是通過TemplateBinding將rectBackground的Fill屬性綁定到RadioButton的Background屬性。 當您在代碼中的其他任何地方更改RadioButton的Background時,rectBackground都會將此筆刷作為其Fill。 我對您的代碼做了一些修改以說明這一點。 使用DataTemplate可以很容易地將其更改為您的模型。
<Window.Resources>
<ControlTemplate x:Key="SubNavButton" TargetType="RadioButton">
<Grid Margin="5,5">
<Rectangle x:Name="rectBackground"
Fill="{TemplateBinding Background}"
Stroke="#FF000000" Width="150" Height="40"
StrokeThickness="1"/>
<TextBlock x:Name="txtContent" HorizontalAlignment="Center"
VerticalAlignment="Center"
FontFamily="Arial" FontSize="16">
<ContentPresenter />
</TextBlock>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fill"
TargetName="rectBackground" Value="Red"/>
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Fill"
TargetName="rectBackground" Value="Pink"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<!-- The Style simulates the same behavior as the DataTemplate-->
<Style TargetType="RadioButton">
<Setter Property="Template"
Value="{StaticResource SubNavButton}"/>
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<RadioButton>one</RadioButton>
<RadioButton>two</RadioButton>
</StackPanel>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.