2010-06-19 53 views
1

我是新來的WPF,我想知道如何重用一些惱人的xaml我必須避免重複。WPF風格/控制模板重用

<Button Cursor="Hand" HorizontalAlignment="Left" Margin="0,0,0,0" x:Name="MyButton" Style="{StaticResource ButtonTemplate}" Width="286" Content="hi!" Focusable="False" IsTabStop="False"/> 
<Button Cursor="Hand" HorizontalAlignment="Left" Margin="0,0,0,0" x:Name="MyButton2" Style="{StaticResource ButtonTemplate}" Width="286" Content="hi 2!" Focusable="False" IsTabStop="False"/> 

我真的很喜歡用這樣的模板:

<Style TargetType="{x:Type Button}" x:Key="ButtonTemplate"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type Button}"> 
         <Grid x:Name="btGrid"> 
          <Path Cursor="Hand" HorizontalAlignment="Left" Stretch="Fill" Stroke="{x:Null}" Opacity="0" x:Name="path"/> 
          <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True" Visibility="Hidden" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top"/> 
         </Grid> 
         <ControlTemplate.Triggers> 
          <EventTrigger RoutedEvent="Button.PreviewMouseLeftButtonDown"> 
           <EventTrigger.Actions> 
            <BeginStoryboard> 
             <Storyboard SlipBehavior="Slip" BeginTime="00:00:00"> 
              <MediaTimeline Source="{Binding StringFormat={}, Path=Name}" Storyboard.TargetName="{Binding StringFormat={}_wma, Path=Name}"/> 
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="{Binding StringFormat=key{}, Path=Name}" Storyboard.TargetProperty="Visibility"> 
      <DiscreteObjectKeyFrame KeyTime="0"> 
       <DiscreteObjectKeyFrame.Value> 
        <Visibility> 
         Visible 
        </Visibility> 
       </DiscreteObjectKeyFrame.Value> 
      </DiscreteObjectKeyFrame> 
     </ObjectAnimationUsingKeyFrames> 
             </Storyboard> 
            </BeginStoryboard> 
           </EventTrigger.Actions> 
          </EventTrigger> 
          <EventTrigger RoutedEvent="Button.PreviewMouseLeftButtonUp"> 
           <EventTrigger.Actions> 
            <BeginStoryboard> 
             <Storyboard> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetName="{Binding StringFormat=key{}, Path=Name}" Storyboard.TargetProperty="Visibility"> 
      <DiscreteObjectKeyFrame KeyTime="0"> 
       <DiscreteObjectKeyFrame.Value> 
        <Visibility> 
         Hidden 
        </Visibility> 
       </DiscreteObjectKeyFrame.Value> 
      </DiscreteObjectKeyFrame> 
     </ObjectAnimationUsingKeyFrames> 
             </Storyboard> 
            </BeginStoryboard> 
           </EventTrigger.Actions> 
          </EventTrigger> 
          <Trigger Property="IsFocused" Value="True"/> 
          <Trigger Property="IsDefaulted" Value="True"/> 
          <Trigger Property="IsMouseOver" Value="True"/> 
          <Trigger Property="IsPressed" Value="True"/> 
          <Trigger Property="IsEnabled" Value="False"/> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

而且我喜歡{綁定的StringFormat = {},路徑= name}爲指向按鈕的名稱,例如「MyButton」,「MyButton2」等。

當我運行此代碼時,出現錯誤「無法凍結此故事板時間線樹以供跨線程使用」。 :/我明白這是因爲我在故事板中使用綁定,對嗎?我不知道該怎麼做才能完成這項工作。

此外,我想使圖像的ToggleVisibility成爲一個模板,它接受一次「可見」和一次「隱藏」值。 在此先感謝!

回答

1

您可以隨時在樣式中定義除Template之外的屬性。

<Style TargetType="{x:Type Button}" 
      x:Key="ButtonTemplate"> 
     <Setter Property="Cursor" Value="Hand" /> 
     <Setter Property="HorizontalAlignment" Value="Left" /> 
     <Setter Property="Margin" Value="0,0,0,0" /> 
     <Setter Property="Width" Value="286" /> 
     <Setter Property="Focusable" Value="False" /> 
     <Setter Property="IsTabStop" Value="False" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Button}"> 
        ... 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

使你的代碼看起來像

<Button x:Name="MyButton" Style="{StaticResource ButtonTemplate}" Content="hi!" /> 
    <Button x:Name="MyButton2" Style="{StaticResource ButtonTemplate}" Content="hi 2!" /> 
+0

你好,感謝您的回覆,但是,這並不工作...我仍然得到同樣的{「不能凍結跨線程使用這個故事板時間表樹。」}例外。 – Rita 2010-06-22 18:54:51

+0

好的, 這條線是什麼意思: decyclone 2010-06-23 06:29:36

0

呀創建與目標類型風格爲按鈕會做的伎倆。

Tip:將所有樣式信息(例如邊框,背景,模板等)寫在代碼的資源部分下並將其應用於控件始終是一種很好的做法。它會提供良好的可讀性。

HTH :)