0

我有树视图,其信息填充有文档结构。每篇文章都由一个 TreeView 节点表示。

目标是引发点击事件,传递标识文档精确部分的密钥并呈现信息

我有3个问题:

1)如何将信息传递给不同的用户控件 2)双击事件有效(仅尝试使用简单的文本框)但不是单击左键... :( 3) 如何打开精确的部分我在树视图上选择的文档并重复该操作。例如:我点击第 3 篇文章,我想要渲染第 3 篇文章的文档,我点击第 5 篇文章等等等等。

下面的代码:

    <UserControl x:Class="UserControls.DocumentViewLaw"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="800"  d:DesignWidth="900"
             xmlns:controls="clr-namespace:Client.UserControls">
     <Grid x:Name="grdTop">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="220"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Column="0" Grid.ColumnSpan="2">
            <TreeView x:Name="treeViewStructure" HorizontalAlignment="Left" Width="200" >
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                        <Border x:Name="bdrTreeViewItem" HorizontalAlignment="Right" BorderThickness="2" Margin="0.5" Padding="1">
                            <TreeViewItem Header="{Binding Text}" x:Name="treeViewItem" HorizontalAlignment="Left" HorizontalContentAlignment="Left">
                            </TreeViewItem>
                        </Border>
                        <HierarchicalDataTemplate.Resources>
                            <Style TargetType="{x:Type TreeViewItem}">
                                   <EventSetter Event="MouseDoubleClick" Handler="OnTreeNodeMouseClick" />
                            </Style>
                            <Style TargetType="{x:Type Border}">
                                <Style.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="BorderBrush" Value="LightBlue" />
                                        <Setter Property="BorderThickness" Value="3" />
                                        <Setter Property="CornerRadius" Value="9" />
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </HierarchicalDataTemplate.Resources>
                        <HierarchicalDataTemplate.Triggers>
                                <Trigger SourceName="treeViewItem" Property="IsMouseOver" Value="True">
                                <Setter TargetName="bdrTreeViewItem" Property="Background" Value="LightGray" />
                                <Setter TargetName="treeViewItem" Property="Foreground" Value="Red" />
                            </Trigger>
                        </HierarchicalDataTemplate.Triggers>
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>
        </StackPanel>
        <StackPanel Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
            <controls:TabDocumentViewLawControl x:Name="topTabLaw" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="0" />
        </StackPanel>
    </Grid>
</UserControl>

代码隐藏:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Client.UserControls
{

    public partial class DocumentViewLaw : UserControl
    {
        public DocumentViewLaw()
        {
            InitializeComponent();
        }

        public void SetTreeViewNodeStructure(IList<TreeViewNode> nodes)
        {
         //this method is recalled in MainWindow.cs where I pass the object returned by 
         // WCF and attached to the TreeView
            this.treeViewStructure.ItemsSource = nodes;
        }

        public void OnTreeNodeMouseClick(object sender, RoutedEventArgs e)
        {
        }
    }
}

第二个用户控制在哪里可视化文档:

<UserControl x:Class="Client.UserControls.TabDocumentViewLawControl"
                 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:editor="clr-namespace:RichEditor;assembly=RichEditor"
                 mc:Ignorable="d"
                 d:DesignHeight="500" d:DesignWidth="500"
                 xmlns:vm="clr-namespace:Domain.Model.Document;assembly=Domain">
      <UserControl.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
      </UserControl.Resources>
      <ScrollViewer Grid.Row="5" Grid.Column="1" MaxHeight="250">
        <StackPanel>
          <FlowDocumentReader x:Name="articoloDocumentLaw"  Grid.Row="1" Document="{Binding Path=FlowDocumentArticle}"
                           Visibility="{Binding Path=HasArticoloVisible, Converter={StaticResource BooleanToVisibilityConverter}}" />
        </StackPanel>
      </ScrollViewer>
</UserControl>

我传递给 UserControl 以在“DocumentViewLaw”用户控件中可视化文档及其结构的对象是结果列表的单个结果 在 MainWindow 组件中,我将数据和相应的上下文相关联。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += MainWindow_Loaded;
        this.login.btnLogin.Click += btnLogin_Click;
        this.tabMainControl.resultListControl.RowSelected += resultListControl_RowSelected;
    }

    void resultListControl_RowSelected(object sender, EventArgs e)
    {
        AutonomySearchResult selectedDocument = (AutonomySearchResult)this.tabMainControl.resultListControl.grdResult.SelectedItem;
        this.tabMainControl.topTabControl.SelectedItem = this.tabMainControl.tabResultList;
        Services.ServicesClient client = new Services.ServicesClient();
        var document = client.GetDocument(selectedDocument.DocKey, true);

        this.tabMainControl.topTabControl.SelectedItem = this.tabMainControl.tabDocumentView;
        this.tabMainControl.tabDocumentView.DataContext = document;

        TreeViewFactory treeFactory = new TreeViewFactory();
        var documentStructure= treeFactory.GetStructure(document.DocumentKey, document.XmlStructure, true);
        this.tabMainControl.documentViewLaw.SetTreeViewNodeStructure(documentStructure);
    }

    public virtual void onResultClick(object sender, RoutedEventArgs e)
    {
    }
}

TreeView 工厂:

public class TreeViewFactory { public IList GetStructure(DocumentKey docKey, string structure, bool loadAllParents) { //使用 LINQ2XML 的业务逻辑 }

    public class TreeViewNode
    {
        public TreeViewNode() { }

        public DocumentKey DocKey { get; set; }

        public string Text { get; set; }

        public IList<TreeViewNode> Children { get; set; }
    }

非常感谢你提前:)

4

1 回答 1

0

1) 如何将信息传递给不同的用户控件?

我假设 中的文章数据TreeView.ItemSource和第二个中的数据UserControl是从视图模型或类中的属性绑定的,该视图模型或类设置为DataContext您的Windowor UserControl。从您的视图模型中,您可以绑定到 的SelectedItem属性TreeView,或者监视INotifyPropertyChanged界面以查看绑定到的属性何时TreeView.ItemsSource更改(由用户)。此时,您可以加载所需的数据并更新绑定到第二个数据的任何属性UserControl

2)双击事件有效(仅尝试使用简单的文本框)但不是单击左键... :(

如果您按照上面的建议进行数据绑定,那么您将不需要Click处理程序,因为您可以在用户直接在视图模型中选择另一个节点时找到它。

3)如何打开我在树视图上选择的文档的精确部分并重复操作。例如:我点击第 3 篇文章,我想要渲染第 3 篇文章的文档,我点击第 5 篇文章等等等等。

我真的不明白你问题的这一部分。您应该能够TreeView在绑定到视图模型时访问所选项目的所有属性。

于 2013-07-22T11:22:53.453 回答