【问题标题】:Databound textbox only updates after I edit it. Using MVVM in wpf form数据绑定文本框仅在我编辑后更新。以 wpf 形式使用 MVVM
【发布时间】:2019-08-02 20:36:54
【问题描述】:

我的文本框是绑定到 Datagrid 的 selecteditem 属性的数据。因此,当用户选择数据网格的不同行时,文本框会使用所选行的值进行更新。数据绑定连接设置正确(几乎)。我说的原因是这样的。 TextBox 值不会更新,除非它单击它然后按任意键。 这是一步一步发生的事情: 1.说文本框的初始值为1。 2. 用户单击列 X 值为 5 的行。 3. 我现在必须单击文本框并键入任意键。一旦我这样做,选定的行值就会出现。 4. 预期结果应该是用户选择行和文本框的值更新没有延迟。

所以本质上,文本框的更新只有在我单击它并进行编辑后才会发生。我尝试过使用不同的 UpdatetoSource 属性,但似乎没有任何效果。任何帮助表示赞赏。 另请注意,这是我在 StackOverflow 上的第一个问题,所以如果我没有遵循某些提问规范,请原谅我(欢迎反馈)

Model Class:
    class Account
      {
       public String AccountNumber{get;set;}
       public String CSSMeteterNumber{get;set;}
       public String CSSSDPNumber { get; set; }
      }

    ViewModel:
class AccountViewModel: INotifyPropertyChanged
    {

        public AccountViewModel()
        {
            Load(); 
        }

        private Account _account;
        public Account Account
        {
            get
            {
                return _account;
            }
            set
            {
                _account = value;
                OnPropertyChange(nameof(_account));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
       public void Load()
        {
            Account a = new Account { AccountNumber = "200", CSSMeteterNumber = "100", CSSSDPNumber = "100" };
            Account = a;
            ObservableCollection<Account> accounts = new ObservableCollection<Account>();
            for(int i=0; i<3;i++)
            {
                accounts.Add(new Account { AccountNumber = "200", CSSMeteterNumber = "100", CSSSDPNumber = "100" });
            }
            AccountCollection = accounts;
        }

public partial class AccountView : Window
    {
        public AccountView()
        {
            InitializeComponent();
            AccountViewModel aVM = new AccountViewModel();
            DataContext = aVM;
            CSS_E_DG.ItemsSource = aVM.AccountCollection.ToList();
        }
    }

AccountView Xaml
<Window x:Class="PlsWork.View.AccountView"
        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:PlsWork.View"
        mc:Ignorable="d"
        Title="AccountView" Height="450" Width="800">
    <StackPanel>

        <TextBox Text="{Binding Path=Account.AccountNumber, UpdateSourceTrigger=PropertyChanged}" Width="200" Height="40" Margin="0 0 0 10"/>

        <DataGrid x:Name="CSS_E_DG" AutoGenerateColumns="False" Width="300" Height="300"  SelectedItem="{Binding Path=Account}" >

        </DataGrid>
    </StackPanel>
</Window>

【问题讨论】:

  • AccountCollection 定义在哪里,您的 Datagrid 未绑定,OnPropertyChange(nameof(_account)); 应该是 OnPropertyChange(nameof(Account));,这里缺少很多内容来帮助恕我直言...
  • CSS_E_DG.ItemsSource = aVM.AccountCollection.ToList(); 如果你想关注mvvm 你不应该那样做... 不要在代码中引用 ui 元素... 你需要在视图模型中定义了一个集合,然后将其绑定到视图元素...
  • private ObservableCollection&lt;Account&gt; _accountCollection; public ObservableCollection&lt;Account&gt; AccountCollection { get; set; } 在Account下定义
  • 您能否更新您的 OP 以将此代码(上方)包含在需要去的地方,而不是作为评论。否则这可能会让其他人感到困惑。
  • @Çöđěxěŕ 你已经回答了这个问题: OnPropertyChange(nameof(Account));是修复。非常感谢。

标签: c# wpf mvvm


【解决方案1】:

OnPropertyChange("Account")

OnPropertyChange("")

在这种情况下,您将更新所有属性

【讨论】:

    【解决方案2】:

    OnPropertyChange(nameof(Account)); 是解决方法。感谢@Çöđěxěŕ(见帖子下的cmets)。 在我有 OnPropertyChange(nameof(_account)); 使用支持字段作为事件 arg 之前。

    【讨论】:

      猜你喜欢
      • 2015-10-16
      • 2012-11-24
      • 2014-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-02
      • 1970-01-01
      相关资源
      最近更新 更多