0
点赞
收藏
分享

微信扫一扫

C# Wpf 个人初学小案例---07 依赖属性 Demo

张宏涛心理 2022-04-03 阅读 96
c#wpf

1.实现效果:在文本框中输入文本,点击submit按钮后,会把信息显示以messageBox的形式显示出来;当点击reset后,会把文本框中输入的文本清空(利用依赖属性而不用INotifyPropertyChanged接口实现,效果和06Demo中效果一样)
2.运行结果(相关注释已写在代码中):
在这里插入图片描述

(1)文件结构:
在这里插入图片描述
2)MainWindow.xaml代码:

<Window x:Class="_02Demo.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:_02Demo"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" x:Name="window">
    <Grid Margin="10">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="15"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="10"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <!-- Row 0 -->
        <TextBlock Text="Your department" Grid.Row="0" Grid.Column="0"/>
        <TextBlock Text=":" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center"/>
        <TextBlock Text="{Binding Department, ElementName=window}" Margin="0 2" Grid.Row="0" Grid.Column="2"/>
        <!-- Row 1 -->
        <TextBlock Text="Your name" Grid.Row="1" Grid.Column="0"/>
        <TextBlock Text=":" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center"/>
        <TextBox Text="{Binding PersonName, ElementName=window, Mode=TwoWay}" Margin="0 2" Grid.Row="1" Grid.Column="2"/>
        <!-- Row 3 -->
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3">
            <Button Content="Submit" Margin="4" Width="80" Click="OnSubmit"/>
            <Button Content="Reset" Margin="4" Width="80" Click="OnReset"/>
        </StackPanel>
    </Grid>
</Window>

MainWindow.xaml.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 _02Demo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public string Department
        {
            get { return "Software Engineering"; }
        }
        public string PersonName//依赖属性用的不是普通属性用的value,而是getvalue和setvalue
        {
            get { return (string)GetValue(PersonNameProperty); }
            set { SetValue(PersonNameProperty, value); }
        }
        //DependencyProperty.Register方法:第一个参数是依赖属性的名字;第二个参数是依赖属性的类型;第三个参数是依赖属性所属的类名,也就是所有者类名;第四个参数是该属性的默认值
        public static readonly DependencyProperty PersonNameProperty = DependencyProperty.Register("PersonName",typeof(string), typeof(MainWindow), new PropertyMetadata(string.Empty));
        private void OnSubmit(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Hello " + PersonName);
        }

        private void OnReset(object sender, RoutedEventArgs e)
        {
            PersonName = string.Empty;
        }
    }
}

举报

相关推荐

0 条评论