【问题标题】:WPF TextBox Binding DesyncWPF 文本框绑定不同步
【发布时间】:2017-09-29 21:39:03
【问题描述】:

我遇到了一个问题,即绑定文本框似乎与基础属性不同步。 第一次更新基础属性时,文本框不会更新。然后,如果基础属性再次更新,则文本框将使用原始更新进行更新。这种滑动窗口行为似乎会随着时间的推移而继续。我在做傻事吗?

我使用的是 .NET 4.7,与 2017 社区相比

MainWindow.xaml

<Window x:Class="TextBoxBugTest.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:TextBoxBugTest"
    mc:Ignorable="d"
    Title="MainWindow" Height="100" Width="225">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50*"/>
            <ColumnDefinition Width="50*"/>
        </Grid.ColumnDefinitions>
        <TextBox Width="55" Height="23" Text="{Binding Test}"/>
        <TextBox x:Name="test2" Grid.Column="1" Width="55" Height="23"/>
        <Button Grid.Row="1" Grid.ColumnSpan="2" Height="23" Width="50"
            Click="Button_Click" Content="update"/>
    </Grid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    MainWindowVM _vm;
    private int counter = 1;

    public MainWindow()
    {
        InitializeComponent();
        _vm = new MainWindowVM();
        DataContext = _vm;
        test2.Text = "test";
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var current = counter++;
        _vm.Test = $"test{current}";
        test2.Text = $"test{current}";
    }
}

MainWindowVM.cs

public class MainWindowVM : INotifyPropertyChanged
{
    private string _test;
    public string Test
    {
        get { return _test; }
        set
        {
            if (value != _test) on_prop_changed();
            _test = value;
        }
    }

    public MainWindowVM()
    {
        Test = "test";
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void on_prop_changed([CallerMemberName] string prop = "")
        => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
}

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    是的,我就是个白痴。

    private string _test;
    public string Test
    {
        get { return _test; }
        set
        {
            if (value != _test) on_prop_changed();
            _test = value;
        }
    }
    

    应该是

    private string _test;
    public string Test
    {
        get { return _test; }
        set
        {
            if (value != _test)
            {
                _test = value;
                on_prop_changed();
            }
        }
    }
    

    【讨论】:

    • 您也可以删除您的问题,而不是写一个答案。由于您只是犯了一个简单的印刷错误,因此答案不太可能对未来的读者有所帮助。
    猜你喜欢
    • 1970-01-01
    • 2013-06-02
    • 2012-11-23
    • 2012-11-24
    • 2015-10-16
    • 2011-09-22
    • 2011-03-29
    • 2010-12-20
    • 2018-01-24
    相关资源
    最近更新 更多