WPF DataContext="{Binding SelectedItem,ElementName=_master}"

<Window x:Class="WpfApp80.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:WpfApp80"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <ListBox 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"/> 
            </Grid.RowDefinitions>
            <TextBlock Margin="6" Text="{Binding ProcessName,StringFormat='Name:\{0\}'}"/>
            <TextBlock Grid.Row="1" Margin="6" Text="{Binding Id,StringFormat='ID:0'}"/>
            <TextBlock Grid.Row="2" Margin="6" Text="{Binding PriorityClass,StringFormat='Priority Class:\{0\}'}"/>
            <TextBlock Grid.Row="3" Margin="6" Text="{Binding Threads.Count,StringFormat='Threads Count:0'}"/>
            <TextBlock Grid.Row="4" Margin="6" Text="{Binding TotalProcessorTime,StringFormat='Processor Time:\{0:G\}'}"/>
            
        </Grid>
    </Grid>
</Window>



//xaml.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 WpfApp80
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = Process.GetProcesses().OrderBy(x=>x.ProcessName);
        }
    }
}

 

Another option is IsSynchronizedWithCurrentItem="True" in main ,and referenced the SelectedItem with slash '/' which to quote.

 //
 // Summary:
 //     Gets or sets a value that indicates whether a System.Windows.Controls.Primitives.Selector
 //     should keep the System.Windows.Controls.Primitives.Selector.SelectedItem synchronized
 //     with the current item in the System.Windows.Controls.ItemsControl.Items property.
 //
 //
 // Returns:
 //     true if the System.Windows.Controls.Primitives.Selector.SelectedItem is always
 //     synchronized with the current item in the System.Windows.Controls.ItemCollection;
 //     false if the System.Windows.Controls.Primitives.Selector.SelectedItem is never
 //     synchronized with the current item; null if the System.Windows.Controls.Primitives.Selector.SelectedItem
 //     is synchronized with the current item only if the System.Windows.Controls.Primitives.Selector
 //     uses a System.Windows.Data.CollectionView. The default value is null.
 [Bindable(true)]
 [Category("Behavior")]
 [TypeConverter("System.Windows.NullableBoolConverter, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")]
 [Localizability(LocalizationCategory.NeverLocalize)]
 public bool? IsSynchronizedWithCurrentItem
 {
     get
     {
         return (bool?)GetValue(IsSynchronizedWithCurrentItemProperty);
     }
     set
     {
         SetValue(IsSynchronizedWithCurrentItemProperty, value);
     }
 }

 

<Window x:Class="WpfApp80.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:WpfApp80"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <ListBox ItemsSource="{Binding }" FontSize="16"
                 DisplayMemberPath="ProcessName" x:Name="_master" IsSynchronizedWithCurrentItem="True" />
        <Grid Grid.Column="1" TextBlock.FontSize="16"  >
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/> 
            </Grid.RowDefinitions>
            <TextBlock Margin="6" Text="{Binding ProcessName,StringFormat='Name:\{0\}'}"/>
            <TextBlock Grid.Row="1" Margin="6" Text="{Binding Id,StringFormat='ID:0'}"/>
            <TextBlock Grid.Row="2" Margin="6" Text="{Binding PriorityClass,StringFormat='Priority Class:\{0\}'}"/>
            <TextBlock Grid.Row="3" Margin="6" Text="{Binding Threads.Count,StringFormat='Threads Count:0'}"/>
            <TextBlock Grid.Row="4" Margin="6" Text="{Binding TotalProcessorTime,StringFormat='Processor Time:\{0:G\}'}"/>
            
        </Grid>
    </Grid>
</Window>

 

posted @ 2024-05-02 14:56  FredGrit  阅读(12)  评论(0编辑  收藏  举报