//xaml <Window x:Class="WpfApp42.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:WpfApp42" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Window.Resources> <local:Person Age="20" Name="Fred" x:Key="p1"/> </Window.Resources> <StackPanel> <Button Content="Click me" Margin="4" Padding="4" FontSize="16" HorizontalAlignment="Left"/> <Button Margin="4" HorizontalAlignment="Left" Padding="4"> <StackPanel Orientation="Horizontal"> <Image Source="pack://application:,,,/cl4.jpg" Width="16" Height="16"/> <TextBlock Text="Copy" Margin="10,0,0,0" VerticalAlignment="Center" FontSize="16"/> </StackPanel> </Button> <Button Content="{StaticResource p1}" FontSize="16" Margin="4" Padding="4" HorizontalAlignment="Left"/> <Button Content="{StaticResource p1}" FontSize="16" Margin="4" Padding="4" HorizontalAlignment="Left"> <Button.ContentTemplate> <DataTemplate> <Grid TextBlock.FontSize="16"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <TextBlock Text="Name:"/> <TextBlock Text="{Binding Name}" Margin="6,0,0,0" Foreground="Blue" Grid.Column="1"/> <TextBlock Text="Age:" Grid.Row="1"/> <TextBlock Text="{Binding Age}" Margin="6,0,0,0" Grid.Row="1" Foreground="Red" Grid.Column="1" TextAlignment="Right"/> </Grid> </DataTemplate> </Button.ContentTemplate> </Button> </StackPanel> </Window> //cs using System; using System.Collections.Generic; 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 WpfApp42 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } } class Person { public int Age { get; set; } public string Name { get; set; } public override string ToString() { return $"{Name} is {Age} years old"; } } }
标签:Windows,instantiate,System,class,instance,using,WPF,MainWindow,public From: https://www.cnblogs.com/Fred1987/p/18120168