2010-10-11 54 views
3

我有一個DataTemplate中的按鈕綁定到我的ViewModel中的命令。該按鈕還有一個EventTrigger,用於啓動隱藏編輯控件(其按鈕屬於其中的一部分)的Storyboard。如何在DataTemplate按鈕中使用Command和EventTrigger?

如果我拿起PreviewMouseDown事件,但Command不會被調用,故事板工作正常。如果我在EventTrigger中拾取MouseDown事件,則該命令有效,但Storyboard不會執行。

如何在單擊按鈕時同時執行命令和故事板?

<Button Content="Save" Command="{Binding SaveCommand}" > 
    <Button.Triggers> 
     <EventTrigger RoutedEvent="Button.PreviewMouseDown"> 
      <EventTrigger.Actions> 
       <BeginStoryboard Storyboard="{DynamicResource sbCloseTitleEdit}"/> 
      </EventTrigger.Actions> 
     </EventTrigger> 
    </Button.Triggers> 
</Button> 
+0

好問題...我懷疑作爲處理的EventTrigger標誌着事件,所以該命令是從來沒有調用。不知道如何解決它,雖然... – 2010-10-11 18:32:24

回答

1

我最終加入了代碼隱藏文件爲我的ResourceDictionary包含的DataTemplate解決粗暴我的問題。我不知道這是可能的,但發現這樣的解釋:

Is it possible to set code behind a resource dictionary in WPF for event handling?

除了使用EventTrigger開始隱藏我添加了一個點擊處理程序的代碼隱藏的編輯控件一個故事板。這有點笨拙,因爲我必須找到相對於單擊按鈕的面板,因爲這是唯一可用的參考,但它可以工作。

如果任何人有一個更好的解決方案仍在尋找更好的解決方案。

0

我試過你的代碼,我發現它可以在PreviewMouseDown上使用事件觸發器正常工作,它只是首先執行該命令,然後動畫會觸發。

我的繼承人資源

<Storyboard x:Key="sbCloseTitleEdit"> 
    <ColorAnimation Storyboard.TargetProperty="(Rectangle.Fill).Color" 
        To="Blue" Duration="0:0:3" Storyboard.TargetName="rect" > 
    </ColorAnimation> 
</Storyboard> 

我的XAML

<Grid> 
    <Grid.ColumnDefinitions> 
    <ColumnDefinition Width="Auto" /> 
    <ColumnDefinition Width="Auto" /> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
    <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 
    <Button Content="Save" Command="{Binding SaveCommand}" > 
    <Button.Triggers> 
     <EventTrigger RoutedEvent="Button.PreviewMouseDown"> 
     <EventTrigger.Actions> 
      <BeginStoryboard 
      Storyboard="{StaticResource sbCloseTitleEdit}"/> 
     </EventTrigger.Actions> 
     </EventTrigger> 
    </Button.Triggers> 
    </Button> 
    <Rectangle Name="rect" Width="30" Height="30" 
      Grid.Column="1" Fill="Red" /> 
</Grid> 

和我的視圖模型

public class MainViewModel 
{ 
    public ActionCommand SaveCommand { get; private set; } 
    public MainViewModel() 
    { 
     SaveCommand = new ActionCommand(); 
    } 
} 

public class ActionCommand : ICommand 
{ 
    public void Execute(object parameter) 
    { 
     // gets fired if event trigger is preview mode 
    } 

    public bool CanExecute(object parameter) 
    { 
     return true; 
    } 

    public event EventHandler CanExecuteChanged; 
} 

你肯定還沒有錯過了什麼?

+0

也許它與使用DataTemplate,因爲我在我的代碼示例? – 2010-12-13 20:52:25

0

我通過與其他動作沿eventtrigger內調用命令來實現這一點,你要觸發:

<Button Command="{Binding AcceptCommand}" Content="Accept"> 
    <i:Interaction.Triggers> 
    <i:EventTrigger EventName="Click"> 
     <i:InvokeCommandAction Command="{Binding AcceptCommand}"/> 
     <triggers:UpdatePropertyAction TargetObject="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:ChildWindow}, Mode=FindAncestor}}" TargetProperty="DialogResult" Value="True" /> 
    </i:EventTrigger> 
    </i:Interaction.Triggers> 
</Button> 
相關問題