0
如何在树视图中绑定按钮与方法,这会给我一个该节点的名称? 到目前为止,我得到这个在节点中的C#WPF TreeView按钮
(“你点击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; }
}
问候
您可以将[ICommand](https://msdn.microsoft.com/en-us/library/system.windows.input.icommand(v = vs.110).aspx)绑定到它:'' – Xiaoy312
RxUI,MvvmLight等各种库都有自己的实现。或者你可以在这里实现你自己的:https://stackoverflow.com/questions/22285866/why-relaycommand – Xiaoy312