从C#中的WPF.xaml访问TreeView



我需要帮助在C#中访问在.xaml中创建的TreeView。我正在创建一个简单的TreeView;Layers";并用它们的名称和其他属性填充TreeView。首先,我只想要名称,然后添加更多属性。

xaml如下:

<Window x:Class="TestWPFAlpha.DocumentStructureWindow"
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:TestAlpha"
mc:Ignorable="d"
Closing="Window_Closing"
Title=" Document Structure" Height="600" MinHeight="600" MaxHeight="600" Width="400" MinWidth="400" MaxWidth="400" ScrollViewer.VerticalScrollBarVisibility="Visible">
<Grid x:Name="gridDocumentStructure" x:FieldModifier="public">
<Menu Height="25" Margin="0,0,0,0" VerticalAlignment="Top" FontSize="14" Background="#FF3C4B64" Foreground="White">
<MenuItem Header="Search" />
<MenuItem Header="Close" Height="25" Click="buttonCloseDocumentStructureWindow_Click"/>
</Menu>
<TreeView x:Name="TreeViewDocumentStructure" x:FieldModifier= "public" Background="#FFEBEBEB" Margin="10,35,10,10">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Members}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</Window>

在C#中,我有:

// all necessary using statements and the two below, that are specific thar I need in this method
using static TestWPFAlpha.DocumentStructureWindow;
using static DocumentLayers.LayerManagerWindow;
...

以及按钮的事件如下:

public static void DocumentStructure_Click(object sender, RoutedEventArgs e)
{
if (documentStructureWindow == null)
{
documentStructureWindow = new TestWPFAlpha.DocumentStructureWindow();
documentStructureWindow.Show();
}
else
{
documentStructureWindow.Activate();
}
var tds = new TestWPFAlpha.DocumentStructureWindow.gridDocumentStructure.TreeViewDocumentStructure();
System.Type canvasType = typeof(System.Windows.Controls.Canvas);
int count = 0;
foreach (UIElement uie in TestWPFAlpha.MainWindow.canvasGrid.Children)
{
if (uie.GetType() == canvasType && count > 0)
{
sb.AppendLine(Layers[count - 1].Name);
TreeViewItem newChild = new TreeViewItem();
// Layers are from DocumentLayers.LayerManagerWindow
newChild.Header = Layers[count - 1].Name;
tds.Items.Add(newChild);
}
count++;
}
}

第49行

var tds = new TestWPFAlpha.DocumentStructureWindow.gridDocumentStructure.TreeViewDocumentStructure();

我得到错误:

类型名称"gridDocumentStructure"在类型"DocumentStructureWindow"中不存在

非常感谢您的帮助。

您请求的示例。这只是背后使用代码的一个基本问题

具有树状视图的窗口:

<Window x:Class="TestWPFAlpha.DocumentStructureWindow"
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:TestWPFAlpha"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid x:Name="gridDocumentStructure" x:FieldModifier="public">
<Menu Height="25" Margin="0,0,0,0" VerticalAlignment="Top" FontSize="14" Background="#FF3C4B64" Foreground="White">
<MenuItem Header="Search" />
<MenuItem Header="Close" Height="25" Click="buttonCloseDocumentStructureWindow_Click"/>
</Menu>
<TreeView x:Name="TreeViewDocumentStructure" x:FieldModifier= "public" Background="#FFEBEBEB" Margin="10,35,10,10">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Members}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>

它创建了一个新窗口:

usings...;
namespace TestWPFAlpha
{
public partial class DocumentStructureWindow : Window
{
public DocumentStructureWindow()
{
InitializeComponent();
var newWin = new Window1(this);
newWin.Show();  
}
}
}

newWin有一个按钮,它可以实现我认为你想要实现的功能:

public partial class Window1 : Window
{
private DocumentStructureWindow _documentStructureWindow;
public Window1(DocumentStructureWindow documentStructureWindow)
{
InitializeComponent();
_documentStructureWindow = documentStructureWindow;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var tds = _documentStructureWindow.TreeViewDocumentStructure;
tds.Items.Add(new TreeViewItem() { Header = DateTime.Now.ToString("yyMMdd_HHmmss_fff")} );
}
}

看到这里:回购

但我推荐mvvm&命令而不是此代码隐藏解决方案

最新更新