节点中的C#WPF TreeView按钮

时间:2017-10-04 19:57:17

标签: c# wpf xaml treeview

如何使用方法绑定treeview中的按钮,这将为我提供该节点的名称? 到目前为止我得到了这个

this

如何编写一个通过MessageBox通知我的方法("你点击了123")?

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; }  
}

此致

1 个答案:

答案 0 :(得分:0)

您可以将ICommand绑定到按钮作为其中一个注释,但默认的DataContext将是您的POCO对象,因此您需要创建一个静态全局对象或使用RelativeSource / FindAncenstor来获取到View或VM ......或者你必须把它放在POCO中。