如何将ObervableCollection或IEnumerable绑定到Flow文档表

问题描述 投票:0回答:1

如何将ViewModel中的ObservableCollection或IEnumerable属性绑定到由Inline FlowDocument定义的表?我不确定这是否是正确的方法。我想从我的视图模型中打印一个Invoice(我认为FlowDocument可能是一种简单的打印方法)。

假设我的ViewModel中有一个属性Items

private ObservableCollection<ItemViewModel> _Items;
    public ObservableCollection<ItemViewModel> Items
    {
        get
        {
            return _Items;
        }
        set
        {
            _Items = value;
            RaisePropertyChanged("Items");
        }
    }

在Xaml中:MainWindow.xaml

....
<FlowDocument>
   <Paragraph>
     <Run Text="Sample text"/>
   </Paragraph>

   ...

   <Table <!-- I cannot find ItemsSource Or anything similar --> >
        <!-- How can i dynamically generate rows and add them here from Items in ViewModel 

             Something like Datagrid's DataGridTextColumn ?
         -->
   </Table>
</FlowDocument>

FlowDocument不是单独的文件或xps。它嵌入在我的MainWindow.xaml文件中

我是初学者,不知道该怎么做。

任何帮助赞赏。

谢谢

c# xaml mvvm flowdocument
1个回答
1
投票

不得不回到我的历史,是的,在我们发现这个article之前我们经历过这个,它说“流程文件中没有数据绑定的支持”。因此我们决定使用itemscontrol而根本不使用FlowDocument。

但这是一个示例解决方法,

MainWindow.xaml

<Window x:Class="WpfApp3.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:vm="clr-namespace:WpfApp3.ViewModel"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Window.DataContext>
        <vm:MainViewModel />
    </Window.DataContext>

    <FlowDocument>
        <Paragraph>
            <Run Text="Sample text"/>
        </Paragraph>
        <Table x:Name="Table1">
        </Table>
    </FlowDocument>

</Window>

MainWindow.xaml.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using WpfApp3.ViewModel;

namespace WpfApp3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainViewModel VM => (MainViewModel) DataContext;

        public MainWindow()
        {
            InitializeComponent();

            BuildTable();
        }

        private void BuildTable()
        {
            foreach (ItemViewModel item in VM.Items)
            {
                TableRow nameRow = BuildRow(item.Name);

                TableRowGroup group = new TableRowGroup();
                group.Rows.Add(nameRow);

                Table1.RowGroups.Add(group);
            }
        }

        private static TableRow BuildRow(string content)
        {
            TextBlock textBlock = new TextBlock
            {
                Text = content
            };

            Block block1 = new BlockUIContainer(textBlock);

            TableCell cell = new TableCell();
            cell.Blocks.Add(block1);

            TableRow row = new TableRow();
            row.Cells.Add(cell);

            return row;
        }
    }
}

ViewModel->MainViewModel.cs

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace WpfApp3.ViewModel
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
            PopulateData();
        }

        private ObservableCollection<ItemViewModel> _Items;
        public ObservableCollection<ItemViewModel> Items
        {
            get => _Items;
            set
            {
                _Items = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #region Stub

        private void PopulateData()
        {
            Items = new ObservableCollection<ItemViewModel>
            {
                new ItemViewModel
                {
                    Name = "Item 1",
                },
                new ItemViewModel
                {
                    Name = "Item 2",
                }
            };
        }

        #endregion
    }
}

ViewModel->ItemViewModel.cs

namespace WpfApp3.ViewModel
{
    public class ItemViewModel
    {
        public string Name { get; set; }
    }
}

输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.