初级入门数据绑定案例
- <Window x:Class="WpfApp1.Data数据绑定.Windowdata"
- 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:WpfApp1.Data数据绑定"
-
- xmlns:s="clr-namespace:WpfApp1.Entity"
-
- mc:Ignorable="d"
- Title="Windowdata" Height="450" Width="800">
- <Grid>
- <Grid.Resources>
-
- <s:Mydata x:Key="mydataSource"/>
- Grid.Resources>
-
- <Grid.DataContext>
- <Binding Source="{StaticResource mydataSource}"/>
- Grid.DataContext>
-
- <Button Background="{Binding Path=ColorName}"
- Width="{Binding Path=Width}" Height="30">
-
- Button>
-
- Grid>
- Window>
C# 类代码 点击事件
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- namespace WpfApp1.Entity
- {
- public class Mydata
- {
- private string colorName = "green";
-
- private int width = 500;
-
- ///
- /// 这样子colorName的属性的默认值就是 "red”
- ///
- // 属性首字母大写啊
- public string ColorName { get => colorName; set => colorName = value; }
-
- public int Width { get => width; set => width = value; }
-
- }
- }
效果