c# - 从嵌套用户控件(MVVM)导出数据

标签 c# wpf xaml mvvm prism

我正在寻找一种更好的方法来从模型获取数据以进行导出。下面概述了我目前的策略,但是我觉得有更好的策略。

假设我有一个NestingView控件,带有一个文本框,按钮和itemscontrol。该按钮将新的NestingView添加到ItemsControl。我的目标是能够导出包括嵌套路径的数据。

具体来说,对于JSON,但这是我认为的重点。作为引用,结果看起来像这样:

{
    "Text": "",
    "Children": []
}

目前,我想出的嵌套控件的方法是让NestingViewModel包含ObservableCollection,供项目控件使用。那么,保存将是遍历集合中的集合的问题……等等。

我相信这是可行的,但是将虚拟机与带有虚拟机的虚拟机混在一起肯定感觉很脏……所以我想知道是否有更好/更容易/更干净/“更多MVVM”的方式来做到这一点。

为简便起见,在此示例中,我没有使用模型,但是假设它在那里,并且包含最终用于导出的经过验证的数据。另外,请注意,我正在使用Prism。

NestingViewModel.cs
public class NestingViewModel : BindableBase
{
    /// <summary>
    /// Initializes a new instance of NestingViewModel
    /// </summary>
    public NestingViewModel()
    {
        NestingViewModels = new ObservableCollection<NestingViewModel>();
        NewNestingView = new DelegateCommand(AddNestingViewModel);
    }

    public ObservableCollection<NestingViewModel> NestingViewModels { get; }

    private String _TextBody;

    /// <summary>
    /// Gets and sets the text body
    /// </summary>
    public String TextBody
    {
        get => _TextBody;
        set => SetProperty(ref _TextBody, value);
    }

    public ICommand NewNestingView { get; }

    /// <summary>
    /// Adds a new NestingViewModel to the collection
    /// </summary>
    private void AddNestingViewModel()
    {
        NestingViewModels.Add(new NestingViewModel());
    }
}

NestingView.xaml
<Border BorderBrush="WhiteSmoke" BorderThickness="5">
<StackPanel Margin="5">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="6*"/>
            <ColumnDefinition Width="4*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <TextBox Grid.Column="0" Text="{Binding TextBody}"/>
        <Button Grid.Column="2" Content="New" Command="{Binding NewNestingView}"/>
    </Grid>
    <ItemsControl Margin="20 5 0 0" ItemsSource="{Binding NestingViewModels}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <local:NestingView/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    </StackPanel>
</Border>

Example

因此,在该示例中,JSON如下所示:
{
    "Text": "Parent",
    "Children": [
        {
            "Text": "ChildA",
            "Children": null
        },
        {
            "Text": "ChildB",
            "Children": [
                {
                    "Text": "ChildB's ChildA",
                    "Children": null
                },
                {
                    "Text": "ChildB's ChildB",
                    "Children": null
                }
            ]
        },
        {
            "Text": "ChildA",
            "Children": null
        }
    ]
}

最佳答案

but it certainly feels dirty having VMs with VMs with VMs



完全没有,那是世界上最正常的事情。我宁愿说没有 child 的 View 模型是不寻常的,除非您有一个非常简单的类似向导的应用程序。

此外,在所有非概念验证应用程序中,我都会避免将原始数据存储在 View 模型中。始终尝试将数据作为模型存储在某些服务中。 View 模型的工作是汇总和处理 View 数据。

关于c# - 从嵌套用户控件(MVVM)导出数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52395717/

相关文章:

c# - 无法使用 Xamarin 在 Android 上创建文件

c# - 如何根据 ASP.NET Core MVC 中用户表的字段添加声明?

wpf - 首先打开新窗口

c# - WPF 中的 Prism 弹出新窗口

c# - 数据未到达重写的 ToString() 方法

c# - 在 asp.net 中打印 PDF,无需打印对话框

wpf - 前缀 'xcdg' 未映射到命名空间

wpf - 如何添加一个在滚动查看器之后始终可见的按钮?

c# - wpf 复选框列表不更新

c# - 使用 XamlReader.Parse() 从字符串读取 xaml 时使用转换器