zoukankan      html  css  js  c++  java
  • C#_Wpf_DataContex上下文整个类绑定

    <Window x:Class="UserStore.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
        <!--g1绑定了Person-->
        <Grid Name="g1">
            <TextBox Height="23" Text="{Binding Name}" HorizontalAlignment="Left" Margin="50,22,0,0" Name="txtName" VerticalAlignment="Top" Width="155" />
            <TextBox Height="23" Text="{Binding Age}" HorizontalAlignment="Left" Margin="50,70,0,0" Name="txtAge" VerticalAlignment="Top" Width="155" />
            <TextBox Height="23" Text="{Binding Height}" HorizontalAlignment="Left" Margin="50,125,0,0" Name="txtHeight" VerticalAlignment="Top" Width="153" />
            <CheckBox Content="性别" IsChecked="{Binding Gender}" Height="16" HorizontalAlignment="Left" Margin="50,178,0,0" Name="chkGender" VerticalAlignment="Top" />
        </Grid>
    </Window>


    //MainWindow.xaml.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    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 UserStore
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            private Person p1 = new Person();
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                p1.Name = "abc";
                p1.Age = 26;
                p1.Height = 170;
                p1.Gender = true;
    
                g1.DataContext = p1;
                
            }
    
          
        }
    }
    
    //Person.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    
    namespace UserStore
    {
        class Person
        {
            public int Age
            {
                get;
                set;
            }
            public string Name
            {
                get;
                set;
            }
            public int Height
            {
                get;
                set;
            }
            public bool Gender
            {
                set;
                get;
            }
        }
    
    }
    


  • 相关阅读:
    Java技术学习笔记:C/S 与B/S 区别
    Java开发面试题总结(八)
    Java技术笔记:数据库的性能优化
    零基础学习Python可以学会吗?你有哪些方法?
    java培训学习路线图之SpringBoot多模块开发学习
    计算机专业选Java和Python哪个前景好点?
    bzoj2152 聪聪可可
    bzoj1468 Tree
    bzoj2879 [Noi2012]美食节
    bzoj2208 [Jsoi2010]连通数
  • 原文地址:https://www.cnblogs.com/MarchThree/p/3720441.html
Copyright © 2011-2022 走看看