[英]Add to Combobox after binding MVVM WPF
Hi after i bind a combobox observablecollection , i want to add from a textbox to combobx.嗨,在绑定组合框 observablecollection 之后,我想从文本框添加到组合框。 adn it give me this error :
它给了我这个错误:
System.NullReferenceException: 'Object reference not set to an instance of an object.' System.NullReferenceException: '未将对象引用设置为对象的实例。'
WpfApp1.MainWindow.getModel(...) returned null. WpfApp1.MainWindow.getModel(...) 返回 null。
Model:模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
namespace WpfApp1
{
public class Parts : Changed
{
public string name;
public string Name
{
get { return name; }
set
{
if (name != value)
{
name = value;
RaisePropertyChanged("Name");
}
}
}
}
}
viewModel:视图模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace WpfApp1
{
public class AddViewModel : Changed
{
private ObservableCollection<Parts> _persons;
public string names;
public AddViewModel()
{
Persons = new ObservableCollection<Parts>()
{
new Parts{Name="Nirav"}
,new Parts{Name="Kapil"}
,new Parts{Name="Arvind"}
,new Parts{Name="Rajan"}
};
}
public ObservableCollection<Parts> Persons
{
get { return _persons; }
set {
if (_persons != value)
{
_persons = value;
RaisePropertyChanged("Persons");
}
}
}
private Parts _sperson;
public Parts SPerson
{
get { return _sperson; }
set {
if (_sperson != value)
{
_sperson = value;
RaisePropertyChanged("SPerson");
}
}
}
}
}
MainWindow:主窗口:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public AddViewModel addviewmodel;
public MainWindow()
{
InitializeComponent();
DataContext = new AddViewModel();
}
public AddViewModel getModel()
{
return addviewmodel;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
getModel().Persons.Add(new Parts { Name = cmbtxt.Text});
}
}
}
XAML: XAML:
<Grid>
<ComboBox ItemsSource="{Binding Persons}" SelectedItem="{Binding Path=SPersons,Mode=TwoWay}" HorizontalAlignment="Left" Margin="391,17,0,0" VerticalAlignment="Top" Width="314" Height="27">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBox Name="cmbtxt" HorizontalAlignment="Left" Height="23" Margin="24,21,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="172" />
<Button Content="Add" HorizontalAlignment="Left" Margin="24,88,0,0" VerticalAlignment="Top" Width="156" Height="49" Click="Button_Click"/>
</Grid>
You could make addviewmodel
a private readonly field and initialize it immediately.您可以将
addviewmodel
私有只读字段并立即对其进行初始化。 Then you simply have to set the DataContext
to the field in the constructor.然后您只需将
DataContext
设置为构造函数中的字段。
Also, getModel()
isn't very C#/.NET friendly.此外,
getModel()
对 C#/.NET 不是很友好。 Use a property if you need to expose the field:如果需要公开字段,请使用属性:
public partial class MainWindow : Window
{
private readonly AddViewModel addviewmodel = new AddViewModel();
public MainWindow()
{
InitializeComponent();
DataContext = addviewmodel;
}
public AddViewModel AddViewModel => addviewmodel;
private void Button_Click(object sender, RoutedEventArgs e)
{
addviewmodel.Persons.Add(new Parts { Name = cmbtxt.Text });
}
}
Using a property you can actually remove the field altogether:使用属性,您实际上可以完全删除该字段:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = AddViewModel;
}
public AddViewModel AddViewModel { get; } = new AddViewModel();
private void Button_Click(object sender, RoutedEventArgs e)
{
AddViewModel.Persons.Add(new Parts { Name = cmbtxt.Text });
}
}
In MainWindow, you never set a value to addviewmodel
, hence it is null.在 MainWindow 中,您永远不会为
addviewmodel
设置值,因此它为空。 You can fix it by changing your constructor:您可以通过更改构造函数来修复它:
public MainWindow()
{
InitializeComponent();
addviewmodel = new AddViewModel()
DataContext = addviewmodel ;
}
Here is a posting about NullReferenceException in general: What is a NullReferenceException, and how do I fix it?这是一篇关于 NullReferenceException 的一般性帖子: What is a NullReferenceException, and how to fix it?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.