WPF简单绑定问题 [英] WPF simple Binding Problem

查看:54
本文介绍了WPF简单绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我既不是约束力也不是WPF的专家.

我在这个非常简单的程序中遇到了问题.
我有3个文本框:
1-名字
2-姓氏
3-全名

当我更改名字或姓氏时,全名文本框不会更新

这是我的程序.请看一下:


WPF:

Hello everyone
I am not expert in neither binding nor WPF.

I have problem in this very simple program.
I have 3 text boxes:
1- First Name
2- Surname
3- Full Name

When I change First Name or Surname, Full Name textbox does not update

this is my program. please have a look at it:


WPF:

<Window x:Class="myTutorial_WPF_Binding_Example.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:src="clr-namespace:WPF_Binding_Example"

        Title="MainWindow" Height="385" Width="563">
    <Window.Resources>
        <src:Person x:Key="myDataSourceObject" PersonFirstName="AAA" PersonSurName="Davis"/>
    </Window.Resources>
    <Grid>
        <TextBlock Margin="10,16,0,0" Text="First Name: " VerticalAlignment="Top" HorizontalAlignment="Left" />
        <TextBlock Margin="18,46,0,0" Text="SurName: " VerticalAlignment="Top" HorizontalAlignment="Left" />
        <TextBlock Margin="14,80,0,0" Text="Full Name: " VerticalAlignment="Top" MaxWidth="Infinity" HorizontalAlignment="Left" />
        <TextBox Margin="79,13,79,0"

                 Text="{Binding Source={StaticResource myDataSourceObject}, Path=PersonFirstName}"

                 VerticalAlignment="Top" />
        <TextBox Margin="79,43,79,0"

                 Text="{Binding Source={StaticResource myDataSourceObject}, Path=PersonSurName}"

                 VerticalAlignment="Top" />
        <TextBox Margin="79,77,79,0"

                 Text="{Binding Source={StaticResource myDataSourceObject}, Path=PersonFullName}"

                 VerticalAlignment="Top" />
    </Grid>
</Window>








这是我的课:








and this is my class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WPF_Binding_Example
{
    public class Person
    {
        private string _personFirstName;
        public string PersonFirstName
        {
            set
            {
                _personFirstName = value;
                PersonFullName = PersonFirstName + " " + PersonSurName;
            }
            get { return _personFirstName; }
        }
        private string _personSurName;
        public string PersonSurName
        {
            set
            {
                _personSurName = value;
                PersonFullName = PersonFirstName + " " + PersonSurName;
            }
            get { return _personSurName; }
        }
        public string PersonFullName { set; get; }
    }
}

推荐答案

您需要在类中实现INotifyPropertyChanged.
在此处阅读喊叫 [此处 [ ^ ].
You need to implement INotifyPropertyChanged in your class.
Read shout this here[^].
Also see here[^].


这篇关于WPF简单绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆