简体   繁体   English

输入时调用C#getter两次

[英]C# getter is called twice on input

Why does the c#-getter get called twice, if i write a letter into the TextBox ? 如果我在TextBox写一个字母,为什么c#-getter会被调用两次?

In my point of view, this is curious, because only one element ( Label ) bind to the property for getting values. 在我看来,这很奇怪,因为只有一个元素( Label )绑定到属性以获取值。

This is my xaml: 这是我的xaml:

<Window x:Class="BindingDebug.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:BindingDebug"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Label Grid.Row="0" Grid.Column="0" Content="Firstname" />
        <TextBox Grid.Row="0" Grid.Column="1"  x:Name="firstNameTextBox" Height="24" VerticalAlignment="Center" HorizontalAlignment="Stretch"
                    Text="{Binding FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

        <Label Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Content="{Binding FirstName}" />
    </Grid>
</Window>

code behind: 代码背后:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = new Model();
    }
}

the model 该模型

public class Model : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string firstName;

    public string FirstName
    {
        get
        {
            System.Diagnostics.Debug.WriteLine("Get: " + firstName ?? "");
            return firstName;
        }

        set
        {
            firstName = value;
            System.Diagnostics.Debug.WriteLine("Set: " + value ?? "");
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
            }
        }
    }
}

Or is it correct that the getter is called twice? 或者两次调用getter是否正确? The debug output goes to the Visual Studio output window. 调试输出转到Visual Studio输出窗口。

Thank you! 谢谢!

Posted by Microsoft on 4/28/2010 at 10:10 AM This is not a bug. 由Microsoft发布于2010年4月28日上午10:10这不是错误。 WPF (or any other code) can call your property-getter at any time for any reason; WPF(或任何其他代码)可以出于任何原因随时致电您的财产获取者; there's no rule that it will be called only once. 没有规则它只会被调用一次。 WPF (and other callers) expects that your property follows the .Net guidelines; WPF(和其他呼叫者)希望您的财产遵循.Net指南; in particular that the property-getter is fast, and that it will return the same value from call to call unless you've raised a property-changed notification. 特别是property-getter很快,并且它会从call到call返回相同的值,除非你引发了属性更改通知。

See here for links etc: WPF Binding View as Content 请参阅此处获取链接等: WPF绑定视图作为内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM