首页 > 其他分享 >WPF DataContext

WPF DataContext

时间:2024-07-05 11:45:48浏览次数:15  
标签:DataContext set get 元素 WPF public

后台代码:

    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }

    }

 

前台代码:

<Window x:Class="BindingTest.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:BindingTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="135" Width="300">
    <StackPanel Background="LightBlue">
        <StackPanel.DataContext>
            <local:Student Id="6" Age="29" Name="Tim"/>
        </StackPanel.DataContext>
        <Grid>
            <StackPanel Name="sp1">
                <TextBox Text="{Binding Id}" Margin="5"/>
                <TextBox Text="{Binding Name}" Margin="5"/>
                <TextBox Text="{Binding Age}" Margin="5"/>
            </StackPanel>
        </Grid>
    </StackPanel>
</Window>

运行结果:

 

结果中的name并没有取父元素的Name(sp1)

注:

在WPF中,DataContext是一个很重要的概念,它是数据驱动UI的核心。DataContext可以设置在任何WPF元素上,并且会被该元素的子元素继承。如果你没有为某个元素显式设置DataContext,那么它会向上查找最近的父元素设置的DataContext。

如果连最近的父元素都没有设置DataContext,那么DataContext默认为null。

标签:DataContext,set,get,元素,WPF,public
From: https://www.cnblogs.com/sound-of-wind-rain/p/18285498

相关文章

  • 学懂C#编程:WPF应用开发系列——WPF之ComboBox控件的详细用法
    WPF(WindowsPresentationFoundation)中的ComboBox控件是一个下拉列表控件,允许用户从一组预定义的选项中选择一个选项。以下是ComboBox控件的详细用法,并附带示例说明。ComboBox的基本用法1.XAML定义:在XAML中定义一个ComboBox控件,并添加一些选项。<Windowx:Class="ComboBox......
  • WPF Performance Suite, Microsoft Windows Performance Toolkit
    Copyfrom https://www.cnblogs.com/lindexi/p/12086719.htmlhttps://learn.microsoft.com/en-us/previous-versions/aa969767(v=vs.110) 1.Downloadurl:  https://download.microsoft.com/download/A/6/A/A6AC035D-DA3F-4F0C-ADA4-37C8E5D34E3D/setup/WinSDKPerformanceT......
  • WPF Datagrid ContextMenu MenuItem Command CommandParameter MultiBinding
     //xaml<Windowx:Class="WpfApp194.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas......
  • 推荐一款Win11主题WPF UI框架
    最近在微软商店,官方上架了新款Win11风格的WPF版UI框架【WPFGalleryPreview1.0.0.0】,这款应用引入了前沿的FluentDesignUI设计,为用户带来全新的视觉体验。WPFGallery简介做为一关注前沿资讯的开发人员,首先关注的是应用WPFGallery到我们的程序中,需要具备的条件:.N......
  • WPF AllowsTransparency DragMove
     //xaml<Windowx:Class="WpfApp191.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas......
  • WPF open image and print as pdf file
    //xaml<Windowx:Class="WpfApp189.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF OpenFielDialog Filter InitialiaDirectory Title
    //xaml<Windowx:Class="WpfApp189.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF Prism PubSubEvent(订阅)
    Prism提供了事件聚合器(EventAggregator)来实现事件的订阅和发布,允许模块之间进行松耦合的通信。主要作用:解耦合:通过事件订阅和发布,模块之间可以实现解耦合,避免直接依赖于彼此的实现细节。示例用法:定义事件类:publicclassMessageEvent:PubSubEvent<string>{}订......
  • WPF进度条中间写百分比数字
    我发现很多同学把思维固话了,通常我们需要实现的进度条是我在网上看到好多例子,但是都没有我的简单,他们不是重写ProcessBar就是使用模板,可以将TextBlock提取出来啊,灵活一点单独绑定然后一句代码Panel.ZIndex="1"就搞定了<StackPanel><ButtonContent="执行耗时......
  • DevExpress WPF中文教程:Grid - 如何显示摘要(设计时)?
    DevExpressWPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpressWPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。无论是Office办公软件的衍伸产品,还是以数据为中心......