WPF中与ViewModel的列表框绑定
我是WPF的新手,正在尝试使用MVVM框架构建一个示例应用程序。我的应用程序有一个xaml文件,其中有一些用于输入客户信息的文本框、用于显示状态的组合框和一个保存按钮。所有的数据绑定都是通过ViewModel(CustomerServiceWMode)完成的,它有一个对模型(Customer)的引用,其中包含必需的字段及其名称 盖特,塞特。viewModel具有CustomerList属性。 单击save按钮后,我想在列表框中显示Customer的FirstName和LastName属性。这就是问题所在。我调试了代码, (单击“代码隐藏”中的“事件”按钮),我可以看到CustomerList具有第一个Customer对象及其所有详细信息,但它没有显示在列表框中。 我的代码是: 客户(模型) } 地址(型号) } 客户服务模型:WPF中与ViewModel的列表框绑定,wpf,Wpf,我是WPF的新手,正在尝试使用MVVM框架构建一个示例应用程序。我的应用程序有一个xaml文件,其中有一些用于输入客户信息的文本框、用于显示状态的组合框和一个保存按钮。所有的数据绑定都是通过ViewModel(CustomerServiceWMode)完成的,它有一个对模型(Customer)的引用,其中包含必需的字段及其名称 盖特,塞特。viewModel具有CustomerList属性。 单击save按钮后,我想在列表框中显示Customer的FirstName和LastName属性。这就是
namespace SampleMVVM.ViewModels
{
class CustomerViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private Customer _customer;
RelayCommand _saveCommand;
private List<String> _stateList = new List<string>();
private string _selectedState;
private ObservableCollection<Customer> _customerList = new ObservableCollection<Customer>();
//public CustomerViewModel(ObservableCollection<Customer> customers)
//{
// _customers = new ListCollectionView(customers);
//}
public Customer CustomerModel
{
get { return _customer; }
set
{
if (value != _customer)
{
_customer = value;
RaisePropertyChanged("CustomerModel");
}
}
}
public List<String> StateList
{
get
{
return _stateList;
}
set { _stateList = value; }
}
public ObservableCollection<Customer> CustomerList
{
get
{
return _customerList;
}
set
{
if (value != _customerList)
{
_customerList = value;
RaisePropertyChanged("CustomerList");
}
}
}
public CustomerViewModel()
{
CustomerModel = new Customer
{
FirstName = "Fred",
LastName = "Anders",
CustomerAddress = new Address
{
AddressLine1 = "Northeastern University",
AddressLine2 = "360, Huntington Avenue",
City = "Boston",
PostalCode = "02115",
Country = "US",
}
};
StateList = new List<String>
{
"Alaska", "Arizona", "California", "Connecticut", "Massachusetts", "New Jersey", "Pennsylvania", "Texas"
};
SelectedState = StateList.FirstOrDefault();
}
public String SelectedState
{
get { return _selectedState; }
set
{
if (value != _selectedState)
{
_selectedState = value;
RaisePropertyChanged(SelectedState);
}
}
}
private void RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
namespace SampleMVVM.ViewModels
{
类CustomServiceWModel:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
私人客户(u客户),;
RelayCommand _saveCommand;
私有列表_stateList=新列表();
私有字符串_selectedState;
私有ObservableCollection_customerList=新ObservableCollection();
//公共客户服务模型(ObservableCollection客户)
//{
//_customers=新列表集合视图(customers);
//}
公共客户客户模型
{
获取{return\u customer;}
设置
{
如果(值!=\u客户)
{
_顾客=价值;
RaisePropertyChanged(“CustomerModel”);
}
}
}
公共列表状态列表
{
得到
{
返回状态列表;
}
设置{u stateList=value;}
}
公共可观察收集客户列表
{
得到
{
退货-客户列表;
}
设置
{
如果(值!=\u客户列表)
{
_客户列表=价值;
RaisePropertyChanged(“客户列表”);
}
}
}
公共CustomerServiceModel()
{
CustomerModel=新客户
{
FirstName=“Fred”,
LastName=“Anders”,
CustomerAddress=新地址
{
AddressLine1=“东北大学”,
AddressLine2=“亨廷顿大道360号”,
城市=“波士顿”,
PostalCode=“02115”,
Country=“美国”,
}
};
StateList=新列表
{
“阿拉斯加”、“亚利桑那州”、“加利福尼亚州”、“康涅狄格州”、“马萨诸塞州”、“新泽西州”、“宾夕法尼亚州”、“德克萨斯州”
};
SelectedState=StateList.FirstOrDefault();
}
公共字符串SelectedState
{
获取{return\u selectedState;}
设置
{
如果(值!=\u selectedState)
{
_selectedState=值;
RaisePropertyChanged(SelectedState);
}
}
}
私有void RaisePropertyChanged(字符串propertyName)
{
var handler=PropertyChanged;
if(处理程序!=null)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
}
CustomerInfo.xaml(视图)
客户信息:
CustomerInfo(代码隐藏类)
namespace SampleMVVM.Views
{
///
///CustomerInfo.xaml的交互逻辑
///
公共部分类CustomerInfo:UserControl
{
公共客户信息()
{
初始化组件();
//校验值();
}
私有void savebtn_单击(对象发送方,路由目标)
{
////客户c=新客户();
////c、 FirstName=fname.Text;
////c、 LastName=lname.Text;
//CustomerViewModel cvm=新CustomerViewModel();
//cvm.CustomerModel.FirstName=fname.Text;
//cvm.CustomerModel.LastName=lname.Text;
//List customerList=新列表();
//customerList.Add(cvm);
var viewModel=DataContext作为CustomerViewModel;
if(viewModel!=null)
{
//viewModel.ShowCustomerInfo();
字符串strfname=viewModel.CustomerModel.FirstName;
字符串strname=viewModel.CustomerModel.LastName;
viewModel.CustomerList.Add(viewModel.CustomerModel);
字符串str1=viewModel.CustomerList.FirstOrDefault().FirstName;
int i=viewModel.CustomerList.Count();
//cList.ItemsSource=viewModel.CustomerList;
}
}
private void组合框\u SelectionChanged(对象发送者,SelectionChangedEventArgs e)
{
CustomerViewModel cvm=新CustomerViewModel();
cvm.SelectedState=ListofStates.SelectedItem.ToString();
}
}
}
我只是不明白我哪里出了问题……有人请帮忙您的问题在这行代码中:
RaisePropertyChanged("CustomerList");
它不适用于集合添加/删除事件。看一看
请记住,在MVVM中,代码隐藏中不应该有太多代码(如果有的话)。考虑使用命令。< p>您只在代码中创建一个CuuleMead对象的新实例(在客户视图模型构造函数中)。因此,您不断更新同一个客户对象,而不是创建新的客户对象 在单击处理程序的末尾,您应该执行
viewModel.CustomerModel = new Customer();
但是
在视图模型中应该有ICommand,而不是单击处理程序
<UserControl x:Class="SampleMVVM.Views.CustomerInfo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ViewModels="clr-namespace:SampleMVVM.ViewModels"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.DataContext>
<ViewModels:CustomerViewModel />
</UserControl.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<!--Starting label-->
<TextBlock FontSize="18" FontFamily="Comic Sans MS" FontWeight="ExtraBlack"
Foreground="Navy"
Grid.Row="0" HorizontalAlignment="Center">
<TextBlock.Text>
Customer Information:
</TextBlock.Text>
</TextBlock>
<TextBlock Text="First name: " Grid.RowSpan="2" HorizontalAlignment="Left" VerticalAlignment="Top"
Grid.Row="1" Width="80px" Height="50px" Margin="40,5,0,0"/>
<TextBox Text="{Binding CustomerModel.FirstName}" Grid.RowSpan="2" HorizontalAlignment="Left"
VerticalAlignment="Top"
Grid.Row="1" Grid.Column="1" Width="80px" Height="20px" Margin="20,5,0,0" Name="fname"/>
<TextBlock Text="Last Name: " Grid.RowSpan="2" HorizontalAlignment="Left" VerticalAlignment="Top"
Grid.Row="2" Width="80px" Height="50px" Margin="40,5,0,0"/>
<TextBox Text="{Binding CustomerModel.LastName}" Grid.RowSpan="2" HorizontalAlignment="Left"
VerticalAlignment="Top"
Grid.Row="2" Grid.Column="1" Width="80px" Height="20px" Margin="20,5,0,0" Name="lname"/>
<TextBlock Text="Address: " Grid.RowSpan="2" HorizontalAlignment="Left" VerticalAlignment="Top"
Grid.Row="3" Width="80px" Height="50px" Margin="40,5,0,0"/>
<TextBox Text="{Binding CustomerModel.CustomerAddress.AddressLine1}" Grid.RowSpan="2" HorizontalAlignment="Left"
VerticalAlignment="Top"
Grid.Row="3" Grid.Column="1" Width="160px" Height="20px" Margin="20,5,0,0"/>
<TextBox Text="{Binding CustomerModel.CustomerAddress.AddressLine2}" Grid.RowSpan="2" HorizontalAlignment="Left"
VerticalAlignment="Top"
Grid.Row="4" Grid.Column="1" Width="160px" Height="30px" Margin="20,5,0,0"/>
<TextBlock Text="City: " Grid.RowSpan="2" HorizontalAlignment="Left" VerticalAlignment="Top"
Grid.Row="5" Width="80px" Height="20px" Margin="40,5,0,0"/>
<TextBox Text="{Binding CustomerModel.CustomerAddress.City}" Grid.RowSpan="2" HorizontalAlignment="Left"
VerticalAlignment="Top"
Grid.Row="5" Grid.Column="1" Width="80px" Height="20px" Margin="20,5,0,0"/>
<TextBlock Text="State: " Grid.RowSpan="2" HorizontalAlignment="Left" VerticalAlignment="Top"
Grid.Row="6" Width="80px" Height="20px" Margin="40,5,0,0"/>
<ComboBox Grid.RowSpan="2" HorizontalAlignment="Left" Name="listOfSates"
VerticalAlignment="Top"
Grid.Row="6" Grid.Column="1" Width="80px" Height="20px" Margin="20,5,0,0"
ItemsSource="{Binding Path=StateList}"
SelectedItem="{Binding Path=SelectedState}"
SelectionChanged="ComboBox_SelectionChanged"
>
</ComboBox>
<TextBlock Text="PostalCode: " Grid.RowSpan="2" HorizontalAlignment="Left" VerticalAlignment="Top"
Grid.Row="7" Width="80px" Height="20px" Margin="40,5,0,0"/>
<TextBox Text="{Binding CustomerModel.CustomerAddress.PostalCode}" Grid.RowSpan="2" HorizontalAlignment="Left"
VerticalAlignment="Top"
Grid.Row="7" Grid.Column="1" Width="80px" Height="20px" Margin="20,5,0,0"/>
<TextBlock Text="Country: " Grid.RowSpan="2" HorizontalAlignment="Left" VerticalAlignment="Top"
Grid.Row="8" Width="80px" Height="20px" Margin="40,5,0,0"/>
<TextBox Text="{Binding CustomerModel.CustomerAddress.Country}" Grid.RowSpan="2" HorizontalAlignment="Left"
VerticalAlignment="Top"
Grid.Row="8" Grid.Column="1" Width="80px" Height="20px" Margin="20,5,0,0"/>
<Button Content="Save" Grid.RowSpan="2" HorizontalAlignment="Left" VerticalAlignment="Top"
Grid.Row="9" Width="50px" Height="20px" Name="savebtn" Margin="40,5,0,0"
Click="savebtn_Click"/>
<ListBox Name="cList" ItemsSource="{Binding Path=CustomerList}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Grid.Row="1" Grid.Column="2" Grid.RowSpan="2" Width="200px" Height="300px" Margin="200,5,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding CustomerModel.FirstName}"
FontWeight="Bold" Foreground="Navy"/>
<TextBlock Text=", " />
<TextBlock Text="{Binding CustomerModel.LastName}"
FontWeight="Bold" Foreground="Navy"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
namespace SampleMVVM.Views
{
/// <summary>
/// Interaction logic for CustomerInfo.xaml
/// </summary>
public partial class CustomerInfo : UserControl
{
public CustomerInfo()
{
InitializeComponent();
//checkvalue();
}
private void savebtn_Click(object sender, RoutedEventArgs e)
{
////Customer c = new Customer();
////c.FirstName = fname.Text;
////c.LastName = lname.Text;
//CustomerViewModel cvm = new CustomerViewModel();
//cvm.CustomerModel.FirstName = fname.Text;
//cvm.CustomerModel.LastName = lname.Text;
//List<CustomerViewModel> customerList = new List<CustomerViewModel>();
//customerList.Add(cvm);
var viewModel = DataContext as CustomerViewModel;
if (viewModel != null)
{
//viewModel.ShowCustomerInfo();
String strfname = viewModel.CustomerModel.FirstName;
String strname = viewModel.CustomerModel.LastName;
viewModel.CustomerList.Add(viewModel.CustomerModel);
String str1 = viewModel.CustomerList.FirstOrDefault().FirstName;
int i = viewModel.CustomerList.Count();
//cList.ItemsSource = viewModel.CustomerList;
}
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
CustomerViewModel cvm = new CustomerViewModel();
cvm.SelectedState = listOfSates.SelectedItem.ToString();
}
}
RaisePropertyChanged("CustomerList");
viewModel.CustomerModel = new Customer();
<TextBlock Text="{Binding FirstName}"
FontWeight="Bold" Foreground="Navy"/>
<TextBlock Text="{Binding LastName}"
FontWeight="Bold" Foreground="Navy"/>
<TextBlock Grid.Row="0"
Grid.Column="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="List of Customers" />
<ListBox Name="cList"
Grid.Row="1"
Grid.RowSpan="8"
Grid.Column="2"
ItemsSource="{Binding CustomerList}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock FontWeight="Bold"
Foreground="Black"
Text="{Binding FirstName}" />
<TextBlock Text=", " />
<TextBlock FontWeight="Bold"
Foreground="Black"
Text="{Binding LastName}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock Grid.Row="10"
Grid.Column="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="{Binding CustomerList.Count,
StringFormat='Total Customers, ={0}'}" />