首页 > 其他分享 >WPF DataContext="{Binding SelectedItem,ElementName=_master}"

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

时间:2024-05-02 15:00:09浏览次数:25  
标签:ElementName Windows SelectedItem Binding System Controls Selector using

<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>

 

标签:ElementName,Windows,SelectedItem,Binding,System,Controls,Selector,using
From: https://www.cnblogs.com/Fred1987/p/18170210

相关文章

  • vscode 快捷件的配置文件地址 C:\Users\Reciter\AppData\Roaming\Code\User\ke
    vscode快捷件的配置文件地址C:\Users\Reciter\AppData\Roaming\Code\User\keybindings.json更改快捷键冲突我要把QuickGoToSelectedFilePath插件的快捷键Ctrl+E,换成F12,插件文章:https://www.cnblogs.com/pengchenggang/p/18163728但是系统里面已经有好几个F12的......
  • 【转载】WPF中Binding使用StringFormat格式化字符串方法
    原文链接:https://www.cnblogs.com/xuliming/articles/StringFormat.htmlWPF中Binding使用StringFormat格式化字符串方法 货币格式<TextBlockText="{BindingPrice,StringFormat={}{0:C}}"/>//$123.46货币格式,一位小数<TextBoxText="{BindingPrice,Stri......
  • WPF深入浅出的 6.3.3 Binding的路径(Path)
    根据WPF的6.3.3Binding的路径(Path)代码验证结果 publicclassCity{publicstringName{get;set;}}publicclassProvince{publicstringName{get;set;}publicList<City>CityList{get;set;}}publ......
  • C++17: 结构化绑定(Structured Bindings)
    遇到有时候写函数需要返回多个值的情况。C++17之前,只能事先定义一个结构体,然后返回这个结构体类型就可以了。但是如果你的这个结构体类型只在这个这里用一次呢。你还要抠脑壳想一个合适的结构体名字,麻烦!std::tupleC++11引入了一个新的类模板叫std::tuple,是一个容器,可以......
  • MultiBinding和Converter用例1
    xaml中使用MultiBinding绑定多个属性<Window.Resources><local:SpecialFeaturesConverterx:Key="SpecialFeaturesConverter"/><local:BooltoVisibilityConvertorx:Key="InputToVisibility"/><ControlTemplatex:Key=&quo......
  • C# implement late binding via type in runtime
    staticvoidRuntimeGetTypeLateBinding(){objects="Hello";PropertyInfopi=s.GetType().GetProperty("Length");Console.WriteLine((int)pi.GetValue(s,null));}  DynamicallycallmethodGetMethod()via reflectionan......
  • 浅谈WPF之Binding时数据校验和类型转换
    在WPF开发中,Binding实现了数据在Source和Target之间的传递和流通,就像现实生活中的一条条道路,建立起了城镇与城镇之间的衔接,而数据校验和类型转换,就像高速公路之间的收费站和安检站。那在WPF开发中,如何实现数据的校验和类型转换呢?本文以一个简单的小例子,简述在WPF开发中,实现数据校......
  • WPF Binding 绑定标记扩展
    参考绑定标记扩展Binding类环境软件/系统版本说明WindowsWindows10专业版22H219045.4046MicrosoftVisualStudioMicrosoftVisualStudioCommunity2022(64位)-17.6.5Microsoft.NetSDK8.0.101手动安装Microsoft.NetSDK7.0.306Mi......
  • 清除缓存解决viewBinding不生效没提示,+ 新手,android stdio,kotlin,viewBinding笔记,2024
    我的问题是缓存导致的,文件->清除缓存->清楚并重启头一次用,我也不知道为什么是缓存问题。官方文档链接:https://developer.android.com/topic/libraries/view-binding?hl=zh-cn官方博客链接:https://medium.com/androiddevelopers/use-view-binding-to-replace-findviewbyid-c......
  • 4.Binding类之转化器和验证规则
    转化器IValueConverter接口Binding类还有一个Converter属性,其实,它是一个IValueConverter接口。它的主要作用是:前后端建立绑定时,定义一套自定义逻辑,让前端显示的数据与后端获取的数据建立一定的对应关系。比如Person对象有一个年龄(Age)属性,我们在前端显示某个人的年龄时,可以根据......