WPF master detail view

FredGrit發表於2024-06-08
<Window x:Class="WpfApp143.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp143"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ListBox Grid.Column="0"  ItemsSource="{Binding}" FontSize="16"
                 DisplayMemberPath="ProcessName" x:Name="_master"/>
        <Grid Grid.Column="1" TextBlock.FontSize="16" DataContext="{Binding SelectedItem,ElementName=_master}">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <TextBlock Foreground="Red" Margin="6" Text="{Binding ProcessName,StringFormat='Name:\{0\}'}"/>
            <TextBlock Foreground="Green" Grid.Row="1" Margin="6" Text="{Binding Id,StringFormat='ID:0'}"/>
            <TextBlock Foreground="Blue" Grid.Row="2" Margin="6" Text="{Binding PriorityClass,StringFormat='Priority Class:\{0\}'}"/>
            <TextBlock Foreground="Orange" Grid.Row="3" Margin="6" Text="{Binding Threads.Count,StringFormat='Threads:0'}"/>
            <TextBlock Foreground="Cyan" Grid.Row="4" Margin="6" Text="{Binding TotalProcessorTime,StringFormat='Processor Time:\{0:G\}'}"/>
        </Grid>
    </Grid>
</Window>


//cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
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 WpfApp143
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = Process.GetProcesses().OrderBy(x=>x.ProcessName);
        }
    }
}

<Grid Grid.Column="1" TextBlock.FontSize="16" DataContext="{Binding SelectedItem,ElementName=_master}">
</Grid>

相關文章