2013-08-02 67 views
0

我正在使用以下樣式集創建自定義wpf控件。在後面的代碼中,我有一個名爲selected的依賴屬性。當OnMouseLeftButtonDown觸發器被執行時,如何將此屬性設置爲true?如何從樣式觸發器設置依賴屬性

<Style TargetType="{x:Type local:myControl}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type local:myControl}"> 
        <ControlTemplate.Resources> 
         <Storyboard x:Key="OnMouseLeftButtonDown"> 
          <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="border"> 
           <EasingColorKeyFrame KeyTime="0" Value="#FF9B1111"/> 
          </ColorAnimationUsingKeyFrames> 
         </Storyboard> 
        </ControlTemplate.Resources> 
        <ControlTemplate.Triggers> 
         <EventTrigger RoutedEvent="UIElement.MouseLeftButtonDown" SourceName="textBlock"> 
          <BeginStoryboard Storyboard="{StaticResource OnMouseLeftButtonDown}"/> 
         </EventTrigger> 
         <EventTrigger RoutedEvent="UIElement.MouseLeftButtonDown" SourceName="textBlock2"> 
          <BeginStoryboard Storyboard="{StaticResource OnMouseLeftButtonDown}"/> 
         </EventTrigger> 
        </ControlTemplate.Triggers> 
        <Border Background="{TemplateBinding Background}" 
          BorderBrush="{TemplateBinding BorderBrush}" 
          BorderThickness="{TemplateBinding BorderThickness}"> 
         <Border Name="border" BorderBrush="#FFDEDEDE" BorderThickness="1" Margin="1" > 
          <StackPanel Orientation="Horizontal"> 
           <TextBlock Name="textBlock" HorizontalAlignment="Left" Margin="2,1,2,3" Text="{TemplateBinding ElementName}" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF7C8CF1" FontSize="10" FontWeight="Bold"></TextBlock> 
           <TextBlock Name="textBlock2" HorizontalAlignment="Left" Margin="2,1,2,3" Text="{TemplateBinding ElementText}" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF75757A" FontSize="10" FontWeight="Bold"></TextBlock> 
          </StackPanel> 
         </Border> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
+0

公共靜態只讀的DependencyProperty SelectedProperty = DependencyProperty.Register(「Selected」,typeof(bool),typeof(XliffElement),new PropertyMetadata(default(bool))); public bool已選擇 { get {return(bool)GetValue(SelectedProperty); } set {SetValue(SelectedProperty,value); } } – John

回答

0

我弄成這樣做的另一種方式。我改變控件的名稱PART_ElementName和PART_ElementText的我的確在OnApplyTemplate()方法

 public override void OnApplyTemplate() 
     { 
      base.OnApplyTemplate(); 
      _elementName = Template.FindName("PART_ElementName", this) as TextBlock; 
      _elementText = Template.FindName("PART_ElementText", this) as TextBlock; 

      if (_elementName != null) _elementName.MouseLeftButtonDown += (sender, args) => SelectControl(); 
      if (_elementText != null) _elementText.MouseLeftButtonDown += (sender, args) => SelectControl(); 
     } 

那麼下面我做的修改在SelectControl()方法

0

在你StoryboardOnMouseLeftButtonDown在您可以添加ObjectAnimationUsingKeyFrames。示例:

<Storyboard x:Key="OnMouseLeftButtonDown"> 
    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="border"> 
     <EasingColorKeyFrame KeyTime="0" Value="#FF9B1111"/> 
    </ColorAnimationUsingKeyFrames> 

    <!-- xmlns:sys="clr-namespace:System;assembly=mscorlib" --> 
    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="YouTarget" Storyboard.TargetProperty="NameOfYourProperty"> 
     <DiscreteObjectKeyFrame KeyTime="0:0:0"> 
      <DiscreteObjectKeyFrame.Value> 
       <sys:Boolean>True</sys:Boolean> 
      </DiscreteObjectKeyFrame.Value> 
     </DiscreteObjectKeyFrame> 
    </ObjectAnimationUsingKeyFrames> 
</Storyboard> 

對於附加依賴屬性必須這樣寫:

Storyboard.TargetProperty="(local:YourDependecyClass.IsSampleMyProperty)" 
+0

好吧,在我的情況下,「YouTarget」會是什麼? – John

+0

@約翰:你爲什麼設置你的財產來源? –

+0

當他們點擊控件上的任何地方時,我就想讓它發生。 – John

相關問題