2009-11-29 49 views
1

使用WPF的MVVM方法,我有一個名爲SubButton的視圖模型類。WPF:從DataTemplate修改ControlTemplate屬性?

<!-- 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不起作用:它如下的樣式。 SelectedSubButton類中的常規.Net屬性。我收到編譯錯誤,因爲編譯器無法確定目標是從哪裏來的。它是ControlTemplate的一部分,我不確定如何告訴它?關於DataContext的一些事情?

回答

1

你想要什麼是不可能的。 WPF與NameScopes協同工作,名稱rectBackground超出了DataTemplate中的範圍。原始名稱rectBackground只會在原始ControlTemplate中的範圍內。這很幸運,因爲否則你將無法在整個應用程序中使用重名。 你可以做的是通過TemplateBinding將rectBackground的Fill屬性綁定到RadioButton的Background屬性。當你在你的代碼的其他地方改變RadioButton的背景時,rectBackground將把這個Brush作爲它的填充。爲了說明這一點,我修改了一下代碼。使用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> 
+0

謝謝,TemplateBinding使得很多更有意義,我不知道如何「外面的世界」的東西都不能進入控制:) 然而,DataTrigger仍然還沒有工作... – evilfred 2009-11-29 18:39:24

+0

我覺得這未設定正確器isChecked(應chnage的背景顏色): <單選按鈕 X:名稱= 「rButton」 背景= 「暗灰」 Template =「{StaticResource SubNavButton}」 GroupName =「{Binding MenuGroup}」/> evilfred 2009-11-29 18:40:02

+0

這是在我的例子中工作:只需更改樣式觸發器中的Setter。問題必須在綁定的Selected屬性中。它是否正確實施更改通知? – Dabblernl 2009-11-29 18:50:24