0

我的文本框是绑定到 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>
4

2 回答 2

2

OnPropertyChange(nameof(Account));是修复。感谢@Çöđěxěŕ(见帖子下的评论)。在我OnPropertyChange(nameof(_account));使用支持字段作为事件 arg 之前。

于 2019-08-02T21:00:35.497 回答
2

OnPropertyChange("Account")

或者

OnPropertyChange("")

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

于 2019-08-03T11:27:50.977 回答