簡體   English   中英

WPF TreeView 屬性為二級

[英]WPF TreeView with properties as second level

我正在使用 Treeview 和以下 Model 對象進行 WPF 項目

PersonModel
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public string FullName
    {
        get { return FirstName + " " + LastName; }
    }


EmployeeModel : PersonModel
    public int MonthlySalary { get; set; }
    public int MonthlyHours { get; set; }

MainWindowViewModel
   public ObservableCollection<EmployeeModel> Employees
    {
        get { return _employees; }
        set { _employees = value; }
    }

主窗口 XAML

    <TreeView ItemsSource="{Binding Employees}">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Employees}">
                <TreeViewItem Header="{Binding FullName}" />
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

所以票價我得到了一份員工名單(全名),我想做的是讓 MonthlySalary 和 MonthlyHours 像孩子一樣:

- John Doe
  - 30 hours
  - 5.000 dollars
- Jane Doe
  - 40 hours
  - 10.000 dollars

關於如何設置 XAML 以便按照我想要的方式列出數據的任何建議? 問候

這里有一些 XAML 給你:

<Window x:Class="WpfApp4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApp4"
    Title="MainWindow"
    Width="800"
    Height="450"
    UseLayoutRounding="True">
    <Window.DataContext>
        <local:MainWindowViewModel />
    </Window.DataContext>
    <Window.Resources>
        <DataTemplate x:Key="HoursTemplate" DataType="{x:Type local:EmployeeModel}">
            <TextBlock>
                <Run Text="{Binding MonthlyHours}" />
                <Run Text=" hours" />
            </TextBlock>
        </DataTemplate>
        <DataTemplate x:Key="SalaryTemplate" DataType="{x:Type local:EmployeeModel}">
            <TextBlock>
                <Run Text="{Binding MonthlySalary}" />
                <Run Text=" dollars" />
            </TextBlock>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <TreeView ItemsSource="{Binding Employees}">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Employees}">
                    <TreeViewItem
                        Header="{Binding FullName}"
                        IsExpanded="True">
                        <TreeViewItem
                            Header="{Binding}"
                            HeaderTemplate="{StaticResource HoursTemplate}" />
                        <TreeViewItem
                            Header="{Binding}"
                            HeaderTemplate="{StaticResource SalaryTemplate}" />
                    </TreeViewItem>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>
</Window>

我希望這有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM