[英]Handling SelectedItemChanged TreeView
I'm trying to get the current selection of a TreeView
. 我正在尝试获取
TreeView
的当前选择。 I want to handle this in my MainWindow.xaml.cs
but the TreeView
is actually in another widow called Window1
in the same namespace. 我想在
MainWindow.xaml.cs
处理此问题,但TreeView
实际上位于同一名称空间中另一个名为Window1
寡妇中。 How can I do this? 我怎样才能做到这一点? Here's what I have so far:
这是我到目前为止的内容:
WPF Code WPF代码
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:LogHunter"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:dxt="http://schemas.devexpress.com/winfx/2008/xaml/grid" x:Class="LogHunter.Window1"
mc:Ignorable="d"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
dx:ThemeManager.Theme="DXStyle"
Title="Window1" Height="750" Width="750">
<Grid>
<TreeView x:Name="treeView" HorizontalAlignment="Left" VerticalAlignment="Top" Width="300" Height="Auto" SelectedItemChanged="clientChanged"/>
</Grid>
</Window>
C# Handler C#处理程序
public void clientChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
System.Diagnostics.Debug.WriteLine(e.OriginalSource);
}
Error I'm getting 我收到错误
'Window1' does not contain a definition for 'clientChanged' and no extension method 'clientChanged' accepting a first argument of type 'Window1' could be found (are you missing a using directive or an assembly reference?)
“ Window1”不包含“ clientChanged”的定义,也找不到找不到接受“ Window1”类型的第一个参数的扩展方法“ clientChanged”(是否缺少using指令或程序集引用?)
You need a way to share the SelectedItem between the two Window: 您需要一种在两个Window之间共享SelectedItem的方法:
1) You can use MVVM and share your viewmodel between your Windows. 1)您可以使用MVVM并在Windows之间共享视图模型。
2) You you need something fast: I suppose you are creating an instance of Window1 from your MainWindow, you can do something like this in your MainWindow: 2)您需要快速执行一些操作:我想您正在从MainWindow创建Window1的实例,您可以在MainWindow中执行以下操作:
Window1 w1 = new Window1();
w1.treeView.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(treeView_SelectedItemChanged);
w1.Show();
Remember to detach this event when you'll destroy Window1. 记住要销毁Window1时,请分离此事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.