我正在使用DevExpress的WPF树状列表视图,并遇到了一个我认为与重命名作为项源使用的对象上的属性有关的更一般问题。在树状列表视图中,需要指定ParentFieldName和KeyFieldName(用于确定树状结构)。这些字段是字符串。
这导致了重构代码的问题。例如,重命名作为ItemSource使用的对象的属性将打破树形视图,因为ParentFieldName和KeyFieldName不再与属性名称同步。我通过在我的视图模型中创建“ParentFieldName”和“KeyFieldName”属性来解决此问题,它们使用nameof 将属性名称呈现给视图。
这是控件的简化版本:
<UserControl
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:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.DataContext>
<ViewModel />
</UserControl.DataContext>
<dxg:TreeListControl AutoGenerateColumns="AddNew"
EnableSmartColumnsGeneration="True" ItemsSource="{Binding Results}"
SelectionMode="Row">
<dxg:TreeListControl.View>
<dxg:TreeListView
ParentFieldName="{Binding ParentIdFieldName}" KeyFieldName="{Binding NodeIdFieldName}"
ShowHorizontalLines="False" ShowVerticalLines="False"
ShowNodeImages="True"/>
</dxg:TreeListControl.View>
</dxg:TreeListControl>
</UserControl>
视图模型:
using DevExpress.Mvvm;
public sealed class ViewModel : ViewModelBase
{
public string ParentIdFieldName => nameof(TreeNode.ParentId);
public string NodeIdFieldName => nameof(TreeNode.NodeId);
public ObservableCollection<TreeNode> Results
{
get => GetProperty(() => Results);
set => SetProperty(() => Results, value);
}
}
并且树节点:
public sealed class TreeNode
{
public int ParentId {get; set;}
public int NodeId {get; set;}
}
我的解决方案可行,但我想知道是否有更好的方法。例如,在XAML中是否有类似于 nameof 调用的东西,而不是绑定到视图模型中的 ParentIdFieldName 和 NodeIdFieldName?
我意识到这可能被描述为DevExpress控件的问题。然而,我对我用来解决这个问题的方法是否可以改进感兴趣。是否有一种更简单的方法可以直接在XAML中完成呢?
如果我提供的代码无法编译,我先向您道歉。我已经大大减少了我正在使用的内容,以提供一个示例。
字典的键不能是'MyNamespace.NameOfExtension'类型。只支持String、TypeExtension和StaticExtension。
- DMX David CardinalPath=
,例如:{Binding {local:NameOf Type={x:Type local:MainWindow}, Member=Title}}
。 - Maxence