6

我是 WPF 新手,正在尝试使用 MVVM 框架构建示例应用程序。我的应用程序有一个 xaml 文件,其中有一些用于输入客户信息的文本框、用于显示状态的组合框和一个保存按钮。所有数据绑定都是通过 ViewModel(CustomerViewMode) 完成的,它引用了 Model(Customer),包含所需的字段及其 Getter、setter。viewModel 有一个 CustomerList 属性。单击保存按钮时,我想在 ListBox 中显示 Customer 的 FirstName 和 LastName 属性。这就是问题所在。我调试了代码,(后面代码中的按钮单击事件),我可以看到 CustomerList 有第一个 Customer 对象及其所有详细信息,但它没有显示在列表框中。我的代码是:客户(模型);

enter code here
namespace SampleMVVM.Models
{
class Customer : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private String _firstName;
    private String _lastName;
    private Address _customerAddress;


    public String FirstName
    {
        get { return _firstName; }
        set
        {
            if (value != _firstName)
            {
                _firstName = value;
                RaisePropertyChanged("FirstName");
            }
        }
    }

    public String LastName
    {
        get { return _lastName; }
        set
        {
            if (value != _lastName)
            {
                _lastName = value;
                RaisePropertyChanged("LastName");
            }
        }
    }

    public Address CustomerAddress
    {
        get { return _customerAddress; }
        set
        {
            if (value != _customerAddress)
            {
                _customerAddress = value;
                RaisePropertyChanged("CustomerAddress");
            }
        }
    }

    private void RaisePropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }


}

}

地址(型号)

namespace SampleMVVM.Models
{
class Address : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private string _addressLine1;
    private string _addressLine2;
    private string _city;
    //private string _selectedState;
    private string _postalCode;
    private string _country;






    public String AddressLine1
    {
        get { return _addressLine1; }
        set
        {
            if (value != _addressLine1)
            {
                _addressLine1 = value;
                RaisePropertyChanged(AddressLine1);
            }
        }
    }

    public String AddressLine2
    {
        get { return _addressLine2; }
        set
        {
            if (value != _addressLine2)
            {
                _addressLine2 = value;
                RaisePropertyChanged(AddressLine2);
            }
        }
    }

    public String City
    {
        get { return _city; }
        set
        {
            if (value != _city)
            {
                _city = value;
                RaisePropertyChanged(City);
            }
        }
    }




    public String PostalCode
    {
        get { return _postalCode; }
        set
        {
            if (value != _postalCode)
            {
                _postalCode = value;
                RaisePropertyChanged(PostalCode);
            }
        }
    }

    public String Country
    {
        get { return _country; }
        set
        {
            if (value != _country)
            {
                _country = value;
                RaisePropertyChanged(Country);
            }
        }
    }

    private void RaisePropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

}

客户视图模型:

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));
        }
    }

        }

}

CustomerInfo.xaml(查看)

<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>

CustomerInfo(类后面的代码)

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();
    }




}

}

我只是不明白我哪里错了......有人请帮忙

4

4 回答 4

3

您只需在代码中(在客户视图模型构造函数中)创建一次 CustomerModel 对象的新实例。因此,您不断更新同一个客户对象,而不是创建一个新对象。

在您的点击处理程序结束时,您应该执行

viewModel.CustomerModel = new Customer();

然而

您应该在视图模型中使用 ICommand 来添加新客户,而不是使用单击处理程序。然后,您应该将按钮的命令绑定到视图模型中的 ICommand。

于 2012-06-07T20:10:23.483 回答
3

对于正确的绑定ListBox.ItemTemplate

<TextBlock Text="{Binding FirstName}"
           FontWeight="Bold" Foreground="Navy"/>
<TextBlock Text="{Binding LastName}"
           FontWeight="Bold" Foreground="Navy"/>

DataContextof已经是ListBoxItem客户了。

于 2012-06-08T00:26:08.277 回答
2

您正在绑定不是 walid 的 CustomerLIst.FirstName,因为 inninterconent 将检查列表框项目源中的属性名称 customerlist。并且因为它不是他们的,所以它会引发一个静默错误,但不会显示到 GUI 中,你需要做的只是提供属性名,如名字和姓氏,这将起作用。

除了你在列表框中的绑定之外,其他一切都很好。只需替换您的列表框绑定,如下所示。

  <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}'}" />

最好是指挥而不是指挥事件。

于 2012-06-08T05:12:04.530 回答
1

您的问题出在这行代码中:

RaisePropertyChanged("CustomerList");

它不适用于集合添加/删除事件。看看ObservableCollection 和 Item PropertyChanged

请记住,在 MVVM 中,您不应该在代码中包含太多代码(如果有的话)。考虑使用命令。

于 2012-06-07T20:03:16.803 回答