【问题标题】:Databinding with unnamed textboxes and listbox使用未命名的文本框和列表框进行数据绑定
【发布时间】:2016-11-14 01:20:42
【问题描述】:

最近有人告诉我,在使用 WPF 和数据绑定时,最好不要命名任何字段,而只是将它们与其他类中的属性相关联。我现在的问题是如何添加来自 3 个文本框(用户输入)的数据,将绑定的信息保存到模型中,然后将帐户信息发布到侧面的列表框中。我需要将数据添加到我的模型中。我的 main.xaml 代码如下:

    <ListBox ItemsSource="{Binding Path=Files}" SelectedItem="{BindingPath=CurrentItem}" />

   <TextBox Text="{Binding Path=bankaccount}"/>
   <TextBox Text="{Binding Path=accountnumber}"/>
   <TextBox Text="{Binding Path=accounttype}"/>
   <Button Content="Save Data To Listbox" Click="Save_Click"/>

现在我将展示我的 FileModel 类,它包含我的所有属性,这些属性将来自文本框

        private short _BankAccount;
        private long _AccountNumber;
        private char _AccountType;

        public short bankaccount{ get { return _BankAccount;} set  {_BankAccount= value;    Notify("bankaccount"); } }
        public long accountnumber{ get { return _AccountNumber;} set {_AccountNumber= value; Notify("accountnumber"); } }
        public char accounttype{ get { return _AccountType;} set{_AccountType= value; Notify("accounttype"); } }

我使用一个名为 ProgramModel 的类作为 Mainpage 和 FileModel 页面之间的中间点,代码如下:

 public class ProgramModel : INotifyPropertyChanged
{

    public ObservableCollection<FileModel> Files { get; set; }

    private FileModel _currentItem;
    public FileModel CurrentItem { get { return _currentItem; } set { _currentItem = value; Notify("CurrentItem"); } }


    public ProgramModel()
    {
        Files = new ObservableCollection<FileModel>();
    }

为了完成它,我有我的主页:

  internal partial class MainWindow
{

    public ProgramModel Model { get; set; }

    private ViewSettings _viewSettings = new ViewSettings();


    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = Model = new ProgramModel();

        }

    private void Save_Click(object sender, RoutedEventArgs e)
    {
        FileModel filemodel = new FileModel();

        Model.Files.Add(new FileModel( filemodel.bankaccount, filemodel.accountnumber, filemodel.accounttype));
    }

我觉得我从保存按钮事件中错误地添加到文件集合中。如果你们能帮助我,那就太好了!所有 3 个文本框和列表框都在主页上。如果您有任何问题,请告诉我。此外,这是一种学习体验,所以如果我发布的代码太多或不够,请告诉我。谢谢!

【问题讨论】:

  • 像bankaccount 这样的属性应该是PascalCased。

标签: c# wpf xaml data-binding listbox


【解决方案1】:

只是一个小小的改变,它应该可以工作。如下所示为 TextBox 更改您的绑定。

<TextBox Text="{Binding Path=CurrentItem.bankaccount}"/>

<TextBox Text="{Binding Path=CurrentItem.accountnumber}"/>

<TextBox Text="{Binding Path=CurrentItem.accounttype}"/>

【讨论】:

    【解决方案2】:

    我不会直接回答你的问题,因为实现正确的数据绑定需要一些代码。

    使用适当的数据绑定,您的 view.cs 上几乎没有代码是可能的! (特别是如果你开始使用框架)

    请查看A Simple MVVM Example,以便您遵循良好做法。

    按照这个例子,你会发现你也可以在按钮和其他控件上使用数据绑定。

    您的视图模型ProgramModel : INotifyPropertyChanged 应该处理所有工作(数据处理)。

    因此,您的模型不应处理 UI 更新通知,

    public short bankaccount{ get { return _BankAccount;} set  {_BankAccount= value;    Notify("bankaccount"); } }
    

    将移至ProgramModel(查看模型)。

    Save_Click 方法也将被转换为 ICommand 并绑定到视图中的按钮,如&lt;Button Content="Save Data To Listbox" Command="{Binding SaveExec}"/&gt;

    关键是,如果您正在研究数据绑定,您应该正确实施它。希望你能理解...

    最后,你的 Main.cs 可能只是..

    internal partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
    
            this.DataContext = new ProgramModel();
        }
    }
    

    【讨论】:

      【解决方案3】:

      您从新的FileModel 实例而不是从绑定到视图的内容中读取值。代码应该是这样的:

      Model.Files.Add(new FileModel
      (
          Model.CurrentItem.bankaccount,
          Model.CurrentItem.accountnumber,
          Model.CurrentItem.accounttype
      ));
      

      确保CurrentItem 实际上是用一个实例初始化的,不要在你的代码中看到它。此外,您可以在此处使用命令并在绑定视图模型中拥有所有相关逻辑,而无需事件。

      另外,现在您将当前项目绑定到ListBox 中的选定项目,这将改为修改现有实例。不确定这是否是有意的。如果您希望这些字段用于输入新实例,请不要将 ListBox 绑定到它。

      【讨论】:

      • H.B.你能进一步解释一下用实例初始化的 CurrentItem 以及我将如何格式化它吗?由于我收到错误消息“对象引用未设置为对象的实例”
      • 只需在视图模型的构造函数中为CurrentItem 分配一个新实例,就像对文件所做的那样。
      猜你喜欢
      • 1970-01-01
      • 2011-09-05
      • 2020-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多