首页 > 其他分享 >WPF instantiate class instance as resource

WPF instantiate class instance as resource

时间:2024-04-07 23:34:25浏览次数:19  
标签:Windows instantiate System class instance using WPF MainWindow public

//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

相关文章

  • WPF WebClient EAP async await
    <Windowx:Class="WpfApp40.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • 往 VisualStudio 工具箱中添加 WPF/WinForms 控件的几种方式
    在使用VisualStudio开发WPF或WinForms应用时,打开UI文件的设计界面,我们可以从工具箱的控件列表中直接拖拽控件到界面上。通过这种方式,可以清晰的展示控件库中所有可用的控件,并且非常方便的将其添加到界面中。那么我们可以通过哪些方式将WPF/WinForms控件库中的控件添加到VisualS......
  • WPF —— 后台实现fromto动画实例
    标签页<ButtonWidth="100"Height="40"Content="点击开始动画"Click="Button_Click"Name="b1"></Button><!--HorizontalAlignment="Left"-->&l......
  • WPF datagrid mvvm multi select via customize datagrid
    usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;namespaceWpfApp39{publicclassMultiSelectDataGrid:D......
  • C#开发之WPF项目中权限控制的实现(attribute)
    1功能描述实现一个权限检查机制,以确保用户根据其权限级别进行相应的操作。定义四级权限:Operator,Maintenance,Supervisor,Administrator,每一级权限都有其特定的操作范围。能够根据用户的权限级别判断用户是否有权执行特定的操作。2设计分析如果实现为接口形式,那么每次在需......
  • WPF布局控件汇总
    1.Grid表格布局Grid为WPF中最常用的布局容器,作为View中的主要组成部分,负责框架中整体的页面布局。注意:Grid的列宽与行高可采用固定、自动、按比例三种方式定义。固定长度:值为一个确定的数字自动长度:值为Auto,实际作用就是取实际控件所需的最小值比例长度:*表示占用剩余的全......
  • WPF开发一个可以自适应排列的Panel控件
    一.控件介绍    初看标题可能无法理解,我们看看什么是自适应排列。乍一看它有点像WrapPanel控件,都是从左至右排列,如果一行排列不下就换行继续排列,但是细看你就会发现不对,WrapPanel控件行尾是不会对齐的,也就是说只要WrapPanel的子控件的宽度不一致,每一行的末尾就会必定留下一......
  • Wpf BackgroundWorker WorkerSupportsCancellation CancellationPending
    //xaml<Windowx:Class="WpfApp37.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • Wpf BackgroundWorker DoWork RunWorkerCompleted
    //xaml<Windowx:Class="WpfApp37.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • WPF登录界面样例
    XAML文件内容如下1<Windowx:Class="ERP.Views.Login"2xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"3xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"4xmlns:d="......