问题描述
我正在 WPF 中开发我的第一个用户控件。我的目标是拥有一个基于 TreeView 的控件,每个条目带有一个复选框 + 图像 + 标签。 在我的应用程序中,这种 TreeView 会出现多次。 除了一件事,我已经做了很多工作: 我最初添加的数据没有显示在用户控件中。
这是我所拥有的: 托管窗口 XAML:
<Window x:Class="Calm.View.Projectmanagementwindow"
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:uc="clr-namespace:Calm.View"
mc:Ignorable="d"
Title="Projectmanagement" Height="450" Width="800">
<Grid>
<Grid.ColumnDeFinitions>
<ColumnDeFinition Width="5" />
<ColumnDeFinition Width="45*" />
<ColumnDeFinition Width="5" />
<ColumnDeFinition Width="60*" />
<ColumnDeFinition Width="5" />
</Grid.ColumnDeFinitions>
<Grid.RowDeFinitions>
<RowDeFinition Height="5"/>
<RowDeFinition Height="*"/>
<RowDeFinition Height="5"/>
</Grid.RowDeFinitions>
<!-- Linke Seite -->
<Grid Grid.Column="1" Grid.Row="1">
<Grid.ColumnDeFinitions>
<ColumnDeFinition Width="50*" />
<ColumnDeFinition Width="50*" />
</Grid.ColumnDeFinitions>
<Grid.RowDeFinitions>
<RowDeFinition Height="*"/>
<RowDeFinition Height="35"/>
<RowDeFinition Height="125"/>
</Grid.RowDeFinitions>
<!-- Todo: Binding not wirking properly -->
<uc:CheckableTreeView Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" x:Name="PMTree"
CheckableItemsSource="{Binding Path=ProjektManagementItems.TreeItems}"/>
<!-- Todo: Rplace Buttons with the Functionality -->
<StackPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Orientation="Horizontal">
<Button Width="150" Margin="3" Click="Button_Click">InsertUserControlContent</Button>
<Button Width="150" Margin="3" Click="Button_Click2">GetCheckedIetms</Button>
</StackPanel>
<!-- Todo: Remove TreeView after usercontrol works -->
<TreeView x:Name="ThisTree" ItemsSource="{Binding Path=ProjektManagementItems.TreeItems}"
Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding ChildItems}">
<StackPanel Orientation="Horizontal">
<CheckBox VerticalAlignment="Center" IsChecked="{Binding IsChecked}"/>
<Image Width="20" Margin="3" Source="{Binding Image}"/>
<TextBlock VerticalAlignment="Center" Text="{Binding Name}"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</Grid>
</Window>
托管窗口 XAML.cs(本质上):
public class Projectmanagementviewmodel : PropertyChangedEventImp
{
private static readonly log4net.ILog _log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#region properties
private CheckableTreeStructContext _projektManagementItems;
/// <summary>
/// This object handles the tree for a checkableTreeView
/// </summary>
public CheckableTreeStructContext ProjektManagementItems { get => _projektManagementItems; }
#endregion
#region constructor
public Projectmanagementviewmodel()
{
try
{
_projektManagementItems = new CheckableTreeStructContext();
// Todo: remove dummyData
LoadTreeViewData();
}
catch (Exception ex)
{
_log.Error("Konstruktor Projectmanagementviewmodel",ex);
throw ex;
}
}
#endregion
#region methods
/// <summary>
/// Use this method to load the necessary data for the tree view into the ProjektManagementItems object
/// </summary>
private void LoadTreeViewData()
{
// Todo: remove dummyData
var tempItemR = new CheckableTreeViewElementBasis("Root1","konzept");
var tempItem1 = new CheckableTreeViewElementBasis("Child1.1","projekt");
var tempItem2 = new CheckableTreeViewElementBasis("Child1.1.1","gruppe");
var tempItem3 = new CheckableTreeViewElementBasis("Child1.2","projekt");
tempItem1.AddChildItem(tempItem2);
tempItemR.AddChildItem(tempItem1);
tempItemR.AddChildItem(tempItem3);
_projektManagementItems.AddItem(tempItemR);
tempItemR = new CheckableTreeViewElementBasis("Root2","konzept");
tempItem1 = new CheckableTreeViewElementBasis("Child2.1","projekt");
tempItem2 = new CheckableTreeViewElementBasis("Child2.1.1","gruppe");
tempItem3 = new CheckableTreeViewElementBasis("Child2.2","projekt");
tempItem1.AddChildItem(tempItem2);
tempItemR.AddChildItem(tempItem1);
tempItemR.AddChildItem(tempItem3);
_projektManagementItems.AddItem(tempItemR);
SetProperty("CheckableTreeStructContext");
}
#endregion
}
CheckableTreeViewElementBasis 当然有属性 ChildItems、IsChecked、Image 和 Name。
(“ThisTree”正是我想要的)
<UserControl x:Class="Calm.View.CheckableTreeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Calm.View"
mc:Ignorable="d"
Name="MyTestName"
d:DesignHeight="450" d:DesignWidth="800">
<!-- Todo: Binding not showing properly -->
<TreeView x:Name="CheckableTree"
Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2" ItemsSource="{Binding CheckableItemsSource}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding ChildItems}">
<StackPanel Orientation="Horizontal">
<CheckBox VerticalAlignment="Center" IsChecked="{Binding IsChecked}"/>
<Image Width="20" Margin="3" Source="{Binding Image}"/>
<TextBlock VerticalAlignment="Center" Text="{Binding Name}"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</UserControl>
用户控制 XAML.cs:
using Calm.viewmodel;
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
namespace Calm.View
{
/// <summary>
/// Interaktionslogik für CheckableTreeView.xaml
/// </summary>
public partial class CheckableTreeView : UserControl
{
private static readonly log4net.ILog _log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#region Addition control properties
/// <summary>
/// DependencyProperty which holds the content for the treeview
/// </summary>
public ObservableCollection<CheckableTreeViewElementBasis> CheckableItemsSource
{
get { return (ObservableCollection<CheckableTreeViewElementBasis>)GetValue(CheckableItemsSourceProperty); }
set
{
SetValue(CheckableItemsSourceProperty,value);
}
}
// Using a DependencyProperty as the backing store for ItemsSourceProperty. This enables animation,styling,binding,etc...
public static readonly DependencyProperty CheckableItemsSourceProperty =
DependencyProperty.Register("CheckableItemsSource",typeof(ObservableCollection<CheckableTreeViewElementBasis>),typeof(CheckableTreeView),new PropertyMetadata(new ObservableCollection<CheckableTreeViewElementBasis>()));
/// <summary>
/// To get access the basefunctionality of the treeview use this property
/// </summary>
public TreeView BaseTree { get { return CheckableTree; } }
#endregion
#region Constructor
public CheckableTreeView()
{
try
{
InitializeComponent();
//viewmodel init
DataContext = this;
}
catch (Exception ex)
{
_log.Error("Konstruktor CheckableTreeView",ex);
MessageBox.Show(ex.Message,GenaralConstants.MessageBoxText_ApplicationError,MessageBoxButton.OK,MessageBoxImage.Error);
}
}
#endregion
}
}
现在非用户控件 treeView "ThisTree" 可以完美地打开 Projectmanagementwindow。
用户控件“PMTree”不显示内容。
在按钮上我有一些测试功能,这清楚地表明,如果
DataContext = this;
未使用,用户控件的内容已填充并反映“ThisTree”的选中状态但不可见。
如果我使用 DataContext = this;
,CheckableItemsSource 在启动时为空。如果我这样做:
PMTree.CheckableItemsSource = ProjektManagementItems.TreeItems
之后 TreeView 剂量出现。
谁能告诉我如何让用户 treeView 在打开 Projectmanagementwindow 时显示? 我不想使用“已加载”事件。
I System 相关:Visual Studio 2019、C# .Net Framework 4.8、Windows 10 64 位。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)