引入命名空间:
xmlns:system="clr-namespace:System;assembly=mscorlib"
XAML:
<Window x:Class="WpfApp.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:system="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WpfApp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="600">
<Window.DataContext>
<ObjectDataProvider ObjectType="local:Welcom">
<ObjectDataProvider.ConstructorParameters>
<system:String>孙悟空</system:String>
<system:Int32>100</system:Int32>
</ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>
</Window.DataContext>
<Grid>
<TextBox Height="20" Width="200" BorderBrush="Black" Text="{Binding Name}"/>
</Grid>
</Window>
CS:
public class Welcom : ObservableObject
{
private string _name = "Hello World!!!";
public string Name
{
get => _name;
set { _name = value; RaisePropertyChanged(nameof(Name)); }
}
public Welcom(string name, int score)
{
_name = name + " " + score.ToString() + " 分";
}
}
视图:
标签:DataContext,string,XAML,构造函数,public,name From: https://www.cnblogs.com/huvjie/p/16852717.html