使用 MVVM 在 WPF 中打印 TreeView [英] Printing the TreeView in WPF using MVVM

查看:41
本文介绍了使用 MVVM 在 WPF 中打印 TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 treeView 可以从文本文件中返回我的文本搜索结果.

I have a treeView to return my text search result from text files.

<TreeView ItemsSource="{Binding FirstGeneration}"
             ...>
  <TreeView.ItemContainerStyle.../>
  <TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Children}">
      <StackPanel Orientation="Horizontal" FlowDirection="LeftToRight">
        <TextBlock Text="{Binding PreExp}" />
        <TextBlock Text="{Binding Exp}"
          FontStyle="{Binding FontStyle}"
          Foreground="{Binding Color}"  />
        <TextBlock Text="{Binding PostExp}" />
      </StackPanel>
    </HierarchicalDataTemplate>
  </TreeView.ItemTemplate>
</TreeView>

为了以树的形式获取结果(因为我们以列表的形式从 C++ 项目中获取结果),我们创建了一个逻辑树并将 exp 显示为红色.我将它们分成三个文本框.

To get the result as a tree (because we get the result from a C++ project as list), we create a logical tree and display the exp in red. I separated them into three textBoxes.

treeView 位于不同的 UserControl 中 - 我将其放入 SearchView (UC) 中.

The treeView is in a diffrent UserControl - and I put it into the SearchView (UC).

现在我想在这棵树上打印所有结果.我更喜欢打印文档并以红色强调搜索结果.

Now I'd like to print all the results on this tree. I prefer that the document is printed with the emphasis on the search result in red.

看起来像这个.

我尝试了 PrintDialog.PrintVisual.问题是我无法访问树或搜索表达式,因为 ViewModel 不知道视图等.

I tried the PrintDialog.PrintVisual. The problem is that I can't reach the tree or the search expression because the ViewModel does not know the view etc.

虽然我在下面这段代码后面的代码中尝试过,但它只打印了他看到的内容,而不是整个树的结果.

Although I tried it in the code behaind this code below and it prints only what he sees and not the entire tree results.

PrintDialog dialog = new PrintDialog();
if (dialog.ShowDialog() != true)
  return;
dialog.PrintVisual(SearchResultTree, "The Search Result Tree");

我也尝试了 FlowDocument 的选项:

Also I tried the option with FlowDocument:

FlowDocument doc = new FlowDocument();
foreach (SearchObjectViewModel item in tv.Items)
  doc.Blocks.Add(new Paragraph(new Run(item.PreExp+item.Exp+item.PostExp)));
pd.PrintDocument(((IDocumentPaginatorSource)doc).DocumentPaginator,exp);

10x 4 帮助!

推荐答案

问题是我无法访问树或搜索表达式,因为 ViewModel 不知道视图等.

The problem is that I can't reach the tree or the search expression because the ViewModel does not know the view etc.

当使用 MVVM 时,View 和 ViewModel 之间的交互应该通过 INotifyPropertyChanged 接口.您可以将一个属性映射/绑定到一个 UI 元素;它在 Set 上得到了回调.

When using MVVM, the interaction between the View and ViewModel should happen via INotifyPropertyChanged Interface. You could map/bind one Property to one UI element; which gets a call back on Set.

这是我关于 mvvm-binding-treeview-item-changed.希望有用.

Here is one of my example on mvvm-binding-treeview-item-changed. Hope it is some use.

这篇关于使用 MVVM 在 WPF 中打印 TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆