2017-10-04 115 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中。