我在使用 WPF 和 Galasoft MVVM 工具包獲得一個簡單的 TreeView 來顯示項目時遇到了一些困難。我一定錯過了一些簡單的東西,但我就是找不到。目前我想要的只是創建一組幾個節點并顯示它們。我什至還沒有編寫任何 RelayCommands 或任何其他實質性內容,因此無需擔心。另外,我認識到我可能需要在某處包含一個 HierarchicalDataTemplate - 大概是替換“TreeView.ItemTemplate”部分。誰能指出我看不到的明顯錯誤?任何幫助將不勝感激!編輯:我已經更新了代碼來修復我愚蠢的缺乏子節點列表的問題,但仍然沒有顯示任何內容。主窗口.xaml:<Window x:Class="Analyzer.MainWindow" 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:ignore="http://www.galasoft.ch/ignore" mc:Ignorable="d ignore" Height="495.333" Width="700" Title="Analyzer" DataContext="{Binding MainViewModel, Source={StaticResource Locator}}"> <Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Skins/MainSkin.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources> <Grid x:Name="LayoutRoot" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"> <TextBlock FontSize="36" FontWeight="Bold" Foreground="Purple" VerticalAlignment="Center" HorizontalAlignment="Center" TextWrapping="Wrap" /> <TreeView ItemsSource="{Binding AllItems}" x:Name="MainTreeView" HorizontalAlignment="Left" Height="408" Margin="10,10,0,0" VerticalAlignment="Top" Width="662"> <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Children}"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path=Name}" /> </StackPanel> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView>
2 回答
手掌心
TA貢獻1942條經驗 獲得超3個贊
我認為你是對的,這兩者之一存在問題;我一定在編輯過程中覆蓋或剝離了一些重要的東西。我發現創建一個新項目后,我在理順事情方面遇到的問題要少得多。
我還必須對我的 XAML 代碼進行以下更改:
<TreeView x:Name="treeView" ItemsSource="{Binding AllItems.Children}" HorizontalAlignment="Left" Height="100" Margin="10,10,0,0" VerticalAlignment="Top" Width="188">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
出于某種原因,我無法將其直接綁定到 AllItems - 我必須將頂級綁定到子項。這省略了我的根節點,但其他一切似乎都有效,所以我現在就用這個。謝謝您的幫助!
- 2 回答
- 0 關注
- 258 瀏覽
添加回答
舉報
0/150
提交
取消
