2012-03-20 84 views
0

我有以下XAML代碼:單擊TreeView項目打開窗口?

<UserControl 
    x:Class="TreeViewWithViewModelDemo.LoadOnDemand.LoadOnDemandDemoControl" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:TreeViewWithViewModelDemo.LoadOnDemand" 
    > 
    <DockPanel> 
     <TreeView ItemsSource="{Binding MyData}"> 

     <TreeView.ItemContainerStyle> 

      <Style TargetType="{x:Type TreeViewItem}"> 
      <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" /> 
      <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> 
      <Setter Property="FontWeight" Value="Normal" /> 
      <Style.Triggers> 
       <Trigger Property="IsSelected" Value="True"> 
       <Setter Property="FontWeight" Value="Bold" /> 
       </Trigger> 
      </Style.Triggers> 
      </Style> 

     </TreeView.ItemContainerStyle> 

     </TreeView> 
    </DockPanel> 
    </UserControl> 

這工作得很好,只要點擊樹形視圖和項目拓展項目和承包。

我想要做的除此之外是當雙擊樹形視圖中的項目時打開一個單獨的窗口。有沒有辦法將這個commmand動作綁定到這個XAML並保持當前的狀態?

感謝

+0

當你說「命令行動」 ......你的意思是你有一個已經打開的窗口中的'ICommand'實施? – 2012-03-20 19:31:22

+0

是的,我有一個ICommand實現,但我不知道如何將它綁定到雙擊樹形視圖... – 2012-03-20 19:43:58

+0

嗨史蒂夫,關於如何將此ICommand綁定到此TreeView的任何想法? – 2012-03-20 20:18:58

回答

0

聽起來像是你想爲你的風格的EventSetter:

 <Style TargetType="{x:Type TreeViewItem}"> 
     <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" /> 
     <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> 
     <EventSetter Event="MouseDown" Handler="TreeViewItem_MouseDown"/> 
     <Setter Property="FontWeight" Value="Normal" /> 
     <Style.Triggers> 
      <Trigger Property="IsSelected" Value="True"> 
      <Setter Property="FontWeight" Value="Bold" /> 
      </Trigger> 
     </Style.Triggers> 
     </Style> 

然後處理事件處理程序打開你的窗口。

+0

我會試試這個......只有一個事件「MouseDown」,沒有「Click」或「Double-Click」? – 2012-03-20 19:55:24

+0

我相信TreeViewItem支持MouseDown和MouseLeftButtonDown,但不能直接雙擊。但是,您可以在處理程序中檢查MouseButtonEventArgs的「ClickCount」屬性,以查看它是雙擊還是單擊。 – 2012-03-20 19:59:59

+0

當我嘗試添加一行時,出現錯誤「試圖導航到事件處理程序時發生未知錯誤,請重建您的項目並重試」。我試圖重建它很多次,它仍然給我這個錯誤。有任何想法嗎?我嘗試「右鍵單擊」並按照「導航到EventHandler」並得到相同的錯誤。 – 2012-03-20 21:30:24

0

你想使用InputBindings屬性如下:

<TreeViewItem> 
    <TreeViewItem.InputBindings> 
     <MouseBinding Gesture="LeftDoubleClick" Command="{Binding Path.To.YourCommand}" /> 
    </TreeViewItem.InputBindings> 
</TreeViewItem> 

這些都沒有提供驗證碼的後面(這是由馬茨回答需要)的優勢,利用ICommand你已經寫了。

我不確定您是否可以在風格中設置它們;你可能需要做的是這樣設置的TreeViewItemTemplate然後用InputBindingsControlTemplate

相關問題