WPF的复杂层次控制

时间:2014-12-24 08:02:34

标签: c# wpf user-interface tree wpf-controls

美好的一天!我刚刚开始学习WPF。有如下构建UI的标准功能吗?在WinForms中,必须为此创建复杂的自定义控件:

enter image description here

WPF中可以使用哪种方法?

3 个答案:

答案 0 :(得分:4)

TreeView可用于此方法。项目容器样式应该如下面的链接那样自定义

http://www.codeproject.com/Articles/17025/Custom-TreeView-Layout-in-WPF

答案 1 :(得分:2)

您也可以使用列表框控件实现此目的。

<强>的Xaml

<Window x:Class="DecoraSnap.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" WindowState="Maximized" >
<Window.Resources>
    <Style  TargetType="ListBoxItem">
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border>
                        <ContentPresenter></ContentPresenter>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<StackPanel Margin="100,0,0,0" Orientation="Horizontal">
    <Border Height="50" Width="100" VerticalAlignment="Center"  BorderBrush="Black" BorderThickness="1">
        <TextBlock  FontWeight="Bold" Text="Meassage" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="0"/>
    </Border>
    <Separator Height="1" Background="Black" VerticalAlignment="Center" Width="50"></Separator>
    <Border x:Name="BorderWidth" Width="1"  Background="Black"   ></Border>
    <ListBox ScrollViewer.VerticalScrollBarVisibility="Disabled" ItemsSource="{Binding RequestList}" BorderBrush="Transparent" BorderThickness="0"   x:Name="lst">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid x:Name="UniformGrid1" Height="{Binding ElementName=lst,Path=ActualHeight}" Loaded="UniformGrid1_Loaded_1"  Columns="1" ></UniformGrid>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition Width="4*"/>
                    </Grid.ColumnDefinitions>
                    <StackPanel Orientation="Horizontal">
                        <Separator Width="50" Height="1" Background="Black"></Separator>
                        <Border Height="50" Width="100"  BorderBrush="Black" BorderThickness="1">
                            <TextBlock  FontWeight="Bold" Text="{Binding Reaquest}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="0"/>
                        </Border>
                    </StackPanel>
                    <StackPanel Grid.Column="1" Orientation="Horizontal">
                        <Border Background="Black" Height="1"  Width="50"></Border>
                        <!--You can implement border width like "UniformGrid1_Loaded_1" event -->
                        <Border Width="1" Background="Black"  Height="235" ></Border>
                        <ListBox   Background="Transparent" HorizontalAlignment="Center" BorderBrush="Transparent" ItemsSource="{Binding Models}">
                            <ListBox.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <UniformGrid x:Name="Uniformgrid2"  Columns="1"></UniformGrid>
                                </ItemsPanelTemplate>
                            </ListBox.ItemsPanel>
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel  Orientation="Horizontal">
                                        <Separator Width="50" Height="1" Background="Black"></Separator>
                                        <Grid Height="40" Width="100" >
                                            <Border  BorderBrush="Black" BorderThickness="1">
                                                <TextBlock  FontWeight="Bold" Text="{Binding Name}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="0"/>
                                            </Border>
                                        </Grid>
                                        <Separator Width="50" Height="1" Background="Black"></Separator>
                                        <Grid Height="100" Width="100" >
                                            <Rectangle Stroke="Black" Fill="LightYellow" StrokeThickness="1" StrokeDashArray="1,2"></Rectangle>
                                            <TextBlock  FontWeight="Bold" Foreground="Silver" Text="{Binding SameName}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="0"/>
                                        </Grid>
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

c#c​​ode

  public partial class MainWindow : Window
{                         
    public MainWindow()
    {
        InitializeComponent();

        List<Meassage> RequestList = new List<Meassage>();

        RequestList.Add(new Meassage()
        {
            Reaquest = "request",
            Models = new List<Commands>(){new Commands(){ Name = "command", SameName="command"},
                                          new Commands(){Name = "Metainfo", SameName="MetaInfo"},
                                          new Commands(){Name = "data", SameName="result" },}
        });

        RequestList.Add(new Meassage()
        {
            Reaquest = "response",
            Models = new List<Commands>(){new Commands(){ Name = "command", SameName="command"},
                                          new Commands(){Name = "Metainfo", SameName="MetaInfo"},
                                          new Commands(){Name = "data", SameName="result" },}
        });

        lst.ItemsSource = RequestList;
    }

    private void UniformGrid1_Loaded_1(object sender, RoutedEventArgs e)
    {
        UniformGrid un = sender as UniformGrid;          
        var ab= un.ActualHeight;
        var ItemsCount = un.Children.Count;
        var SingleHeight = ab/ItemsCount;
        BorderWidth.Height = SingleHeight * (ItemsCount - 1);
    }    

}

public class Meassage
{
    public string Reaquest { get; set; }

    public List<Commands> Models { get; set; }
}

public class Commands
{
    public string Name { get; set; }
    public string SameName{ get; set; }

}

结果

enter image description here

答案 2 :(得分:1)

正如XAML Lover所提到的,TreeView控件将是更好的选择。您可以将它与HierarchicalDataTemplate一起使用,您可以在其中设置ItemsSource并绑定到视图模型。

http://msdn.microsoft.com/en-us/library/dd759035(v=vs.95).aspx

链接中的示例使用简单的TextBlock来显示项目标题。如果必须为树视图项添加其他项(如上下文菜单),则可以在HieraichalDataTemplate中添加。然后,要更新外观,您必须自定义TreeViewItem样式或ItemsContainerStyle。您可以在此处添加图标,如果需要在加载时保持树打开,还可以自定义IsExpanded属性。树视图还允许您在控件中导航并从节点中查找子项。