如何在WPF中的HierarchicalDataTemplate中放置自定义用户控件?

时间:2015-11-06 19:27:24

标签: wpf xaml

我已经设置了自定义树视图,并且我从另一个项目获得了一个UserControl,我想将其用作树的节点/节点。这就是我目前所拥有的:

<Window x:Class="Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="GraphStyle.xaml"/>
        </ResourceDictionary.MergedDictionaries>

        <XmlDataProvider x:Key="nodes" Source=".\nodes.xml" XPath="Node"/>

        <HierarchicalDataTemplate DataType="Node" ItemsSource="{Binding XPath=Children/Node}">
            <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" 
         mc:Ignorable="d" Width="200" Height="60">
                <Grid Background="Red" Height="35" VerticalAlignment="Top" Margin="5,0,5,0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="38*"/>
                        <ColumnDefinition Width="125*"/>
                        <ColumnDefinition Width="37*"/>
                    </Grid.ColumnDefinitions>
                    <TextBox x:Name="textBox" Grid.Column="1" TextWrapping="Wrap" Text="TextBox" TextAlignment="Left" Height="35"/>
                    <Ellipse x:Name="buttonAddQuestionNode" Grid.Column="1" Height="12" Margin="57,45,56,-22" Stroke="Black" VerticalAlignment="Top" Width="12">
                        <Ellipse.Fill>
                            <ImageBrush ImageSource="/BlogSample;component/Icons/addIcon.png"/>
                        </Ellipse.Fill>
                    </Ellipse>
                    <TextBlock x:Name="textBlock" TextWrapping="Wrap" Text="A" TextAlignment="Center" FontSize="25" Height="35"/>
                    <Path Grid.Column="1" Data="M100,35 L100,45" Fill="#FFF4F4F5" Height="11" Margin="62,0,62,-11" Stretch="Fill" Stroke="Black" VerticalAlignment="Bottom"/>
                </Grid>
            </UserControl>

        </HierarchicalDataTemplate>

    </ResourceDictionary>
</Window.Resources>

<TreeView ItemContainerStyle="{StaticResource GraphStyle}" 
          ItemsSource="{Binding Source={StaticResource nodes}}"/>
</Window>

HierarchicalDataTemplate下的部分是我将usercontrol作为节点放置的地方。我的问题是有一种方法可以简单地使用像这样的代码来引用usercontrol吗?或者有更好的方法来做到这一点我不知道吗?

1 个答案:

答案 0 :(得分:1)

首先,您应该在项目中创建UserControl.xaml。例如,您创建了这样的UserControl:

<UserControl x:Class="WpfApplication1.SomeUserControl"
             <!--The code omitted for the brevity-->
             <!--Your code of your UserControl from the MainWindow-->
     >
    <Grid Background="Red" Height="35" VerticalAlignment="Top" Margin="5,0,5,0">
        <!--The code omitted for the brevity-->
    </Grid>
</UserControl>

然后在HierarchicalDataTemplate所在的窗口中,您应该在具有UserControl的命名空间中写下短名称。例如,我的项目名为&#34; WPFApplication1&#34; namespve的简称是&#34; local&#34;

<Window x:Class="WpfApplication1.MainWindow"        
    xmlns:local="clr-namespace:WpfApplication1"
    <!--The code omitted for the brevity-->
    Title="MainWindow" Height="350" Width="525">
 <Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="GraphStyle.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <XmlDataProvider x:Key="nodes" Source=".\nodes.xml" XPath="Node"/>
        <HierarchicalDataTemplate x:Key="aaa">                
           <localUC:SomeUserControl/>
        </HierarchicalDataTemplate>         
    </ResourceDictionary>
</Window.Resources>

<强>更新 你可以吗?

<TreeView>
        <TreeView.Resources>
            <Style TargetType="TreeViewItem">
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <local:SomeUserControl/>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="ItemsSource" Value="{Binding ItemsSource}"/>
                <Setter Property="IsExpanded" Value="True"/>
            </Style>
        </TreeView.Resources>
        <TreeViewItem Header="1">
            <TreeViewItem Header="21">
                <TreeViewItem Header="31">
                    <TreeViewItem Header="41">
                        <TreeViewItem Header="51">
                            <TreeViewItem Header="61">
                                <TreeViewItem Header="71">

                                </TreeViewItem>
                            </TreeViewItem>
                        </TreeViewItem>
                    </TreeViewItem>
                </TreeViewItem>
            </TreeViewItem>
        </TreeViewItem>

    </TreeView>