2017-10-04 113 views
0

如何在樹視圖中綁定按鈕與方法,這會給我一個該節點的名稱? 到目前爲止,我得到這個在節點中的C#WPF TreeView按鈕

this

(「你點擊123」)如何寫一個通知我,例如用MessageBox的方法?

XAML:

<TreeView Name="trvDevices" HorizontalAlignment="Right" Margin="0,135,9,16" Width="193" > 
        <TreeView.Resources> 
         <HierarchicalDataTemplate DataType="{x:Type self:WirelessCable}" ItemsSource="{Binding Members}"> 
          <StackPanel Orientation="Horizontal"> 
           <Image Margin="0,0,5,0" /> 
           <TextBlock Text="{Binding Name}" /> 
           <TextBlock Text=" [" Foreground="Blue" /> 
           <TextBlock Text="{Binding Members.Count}" Foreground="Blue" /> 
           <TextBlock Text="]" Foreground="Blue" /> 

          </StackPanel> 
         </HierarchicalDataTemplate> 
         <DataTemplate DataType="{x:Type self:Device}"> 
          <StackPanel Orientation="Horizontal"> 
           <Image Margin="0,0,5,0" /> 
           <TextBlock Text="{Binding ID}" /> 
           <TextBlock Text=" - " /> 
           <TextBlock Text="{Binding ConnectionStatus}"/> 
           <TextBlock Text=", " /> 
           <TextBlock Text="{Binding Armed}"/> 
           <Button x:Name="btnTRV" Content="Click" Width="40" Height="20"></Button> 
          </StackPanel> 
         </DataTemplate> 
        </TreeView.Resources> 
       </TreeView> 

CS:

public class WirelessCable 
{ 
    public WirelessCable() 
    { 
     this.Members = new ObservableCollection<Device>(); 
    } 

    public string Name { get; set; } 

    public ObservableCollection<Device> Members { get; set; } 
} 
public class Device 
{ 
    public int ID { get; set; } 
    public string Armed { get; set; } 
    public string ConnectionStatus { get; set; } 
} 

問候

+0

您可以將[ICommand](https://msdn.microsoft.com/en-us/library/system.windows.input.icommand(v = vs.110).aspx)綁定到它:'

+0

RxUI,MvvmLight等各種庫都有自己的實現。或者你可以在這裏實現你自己的:https://stackoverflow.com/questions/22285866/why-relaycommand – Xiaoy312

回答

0

可以綁定的ICommand到按鈕的意見一說,但默認的DataContext將是你的POCO對象,所以你需要創建一個靜態全局對象或使用RelativeSource/FindAncenstor來訪問View或VM ......否則你必須把它放在POCO中。