WPF ComboBox抛出异常

时间:2014-08-03 13:56:20

标签: c# wpf xaml mvvm

以下是我的文件。当我因ComboBox运行命令时,它会抛出一些奇怪的运行时异常,如下所示。

enter image description here

如果没有ComboBox,它运行良好。

如何改进我的代码,使其开始使用ComboBox?

VehicalForm.xaml

        <Window x:Class="Seris.VehicalForm"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="500" Width="600">
<Control>
    <Control.Template>
        <ControlTemplate>
            <WrapPanel Orientation="Vertical" Margin="10 " >
                <Label Content="Vehical No" HorizontalAlignment="Left"/>
                <TextBox Name="VehicalNo_Text" Height="23" Width="80" TextWrapping="Wrap" Text="{Binding VehicalNo}"  HorizontalAlignment="Left" />
                <Label Content="Model" HorizontalAlignment="Left"/>
                <TextBox Name="Model_Text" Height="23" Width="80" TextWrapping="Wrap" Text="{Binding Model}" HorizontalAlignment="Left" />
                <Label Content="Manufacturing Date" HorizontalAlignment="Left"/>
                <DatePicker Name="ManufacturingDate_DateTime" SelectedDate="{Binding ManufacturingDate, Mode=TwoWay}"/>
                <Label Content="IU No" HorizontalAlignment="Left"/>
                <TextBox Height="23" Width="80" Name="IUNO_Text" TextWrapping="Wrap" Text="{Binding IUNo}" HorizontalAlignment="Left"/>
                <Label Content="Personnel" HorizontalAlignment="Left"/>
                <ComboBox Name="Personnel_Combo" Loaded="{Binding Personnel_Combo_Loaded}" HorizontalAlignment="Left" Width="116"/>
                <Separator Height="20" RenderTransformOrigin="0.5,0.5" Width="16"/>
                <Button Name="Save_Button" Command="{Binding SaveButton_Command}" Content="Save" Width="66"/>
                <Label x:Name="Error_Label" Content="{Binding ErrorMessage, UpdateSourceTrigger=PropertyChanged}" Foreground="Red" HorizontalAlignment="Left" Height="41" Width="137"/>
                <ListView Name ="Grid" Height="294" Width="371" >
                    <DataGrid Name="DG" ItemsSource="{Binding ListItems, UpdateSourceTrigger=PropertyChanged}" SelectionUnit="Cell" GridLinesVisibility="None" IsReadOnly="True" AutoGenerateColumns="False" BorderThickness="0">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Vehical No" Binding="{Binding VehicalNo}" />
                            <DataGridTextColumn Header="Model" Binding="{Binding Model}" />
                            <DataGridTextColumn Header="ManufacturingDate" Binding="{Binding ManufacturingDate}" />
                            <DataGridTextColumn Header="IUNo" Binding="{Binding IUNo}" />
                            <DataGridTextColumn Header="Personnel" Binding="{Binding Personnel}" />

                        </DataGrid.Columns>

                    </DataGrid>
                </ListView>
                <Label Name="Notification" Content="hjgj"/>

            </WrapPanel>
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding IsMouseOver, ElementName=Grid}" Value="true">
                    <Setter Property="Content" TargetName="Notification" Value="abc"/>


                </DataTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Control.Template>
</Control>
</Window>

VehicalMainViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Seris.Models;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Seris.Commands;
using Seris.ViewModels;
using System.Windows;
using System.Windows.Controls;

namespace Seris.ViewModels
{
public class VehicalMainViewModel : ObservableObject
{
public ObservableCollection<VehicalModel> _listItems ;

private string _erroMesage;

public string ErrorMessage
{
    get { return _erroMesage; }
    set { _erroMesage = value; OnPropertyChanged("ErrorMessage"); }
}

public ObservableCollection<VehicalModel> ListItems
{
    get { return _listItems; }
    set
    {
        if (!value.Equals( _listItems))
        {
            _listItems = value;
        }
    }
}
    #region Getter-Setter
    private string _VehicalNo;

    public string VehicalNo 
    {
        get { return _VehicalNo; }
        set
        {
            if (value != _VehicalNo)
            {
                _VehicalNo = value.Trim();
                OnPropertyChanged("VehicalNo");

            }
        }
    }
    private string _Model;

    public string Model
    {
        get { return _Model; }
        set
        {
            if (value != _Model)
            {
                _Model = value.Trim();
                OnPropertyChanged("Model");
            }
        }
    }
    private DateTime? _ManufacturingDate;

    public DateTime? ManufacturingDate
    {
        get { return _ManufacturingDate; }
        set
        {
            if (value != _ManufacturingDate)
            {
                _ManufacturingDate = value;
                OnPropertyChanged("ManufacturingDate");
            }
        }
    }
    private string _IUNo;

    public string IUNo
    {
        get { return _IUNo; }
        set
        {
            if (value != _IUNo)
            {
                _IUNo = value.Trim();
                OnPropertyChanged("IUNo");
            }
        }
    }
    private ObservableCollection<string> _PersonnelName;

    public ObservableCollection<string> PersonnelName
    {
        get { return _PersonnelName; }
        set
        {
            if (value != _PersonnelName)
            {
                _PersonnelName = value;
                OnPropertyChanged("PersonnelName");
            }
        }
    } 
    #endregion

    private ICommand _saveButton_Command;

    public ICommand SaveButton_Command
    {
        get { return _saveButton_Command; }
        set { _saveButton_Command = value; }
    }



    public void Personnel_Combo_Loaded(object sender, RoutedEventArgs e)
    {
        var Personnel_ComboBox = sender as ComboBox;
        Personnel_ComboBox.ItemsSource = PersonnelName;
    }
    public void SaveToList(object o1)
    {
        ErrorMessage="";
        try
        {
            _listItems.Add(new VehicalModel(VehicalNo, Model, ManufacturingDate, IUNo, PersonnelName));
        }
        catch(Exception ex)
        {
            ErrorMessage = ex.Message;
        }
    }
    public void RemoveFromList()
    {

    }
    public VehicalMainViewModel()
    {
        ListItems = new ObservableCollection<VehicalModel>();
        PersonnelName = new ObservableCollection<string>() { "sd", "ad", "ad" };
        ErrorMessage = "";
        VehicalModel vm=new VehicalModel();
        SaveButton_Command = new RelayCommand(new Action<object>(SaveToList));

    }
}
}

VehicalModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;

namespace Seris.Models
{
public class VehicalModel : ObservableObject
{
    #region Getter-Setter
    private string _VehicalNo;

    public string VehicalNo 
    {
        get { return _VehicalNo; }
        set
        {
            if (value != _VehicalNo)
            {
                _VehicalNo = value.Trim();
                OnPropertyChanged("VehicalNo");
            }
        }
    }
    private string _Model;

    public string Model
    {
        get { return _Model; }
        set
        {
            if (value != _Model)
            {
                _Model = value.Trim();
                OnPropertyChanged("Model");
            }
        }
    }
    private DateTime? _ManufacturingDate;

    public DateTime? ManufacturingDate
    {
        get { return _ManufacturingDate; }
        set
        {
            if (value != _ManufacturingDate)
            {
                _ManufacturingDate = value;
                OnPropertyChanged("ManufacturingDate");
            }
        }
    }
    private string _IUNo;

    public string IUNo
    {
        get { return _IUNo; }
        set
        {
            if (value != _IUNo)
            {
                _IUNo = value.Trim();
                OnPropertyChanged("IUNo");
            }
        }
    }
    private ObservableCollection<string> _PersonnelName;

    public ObservableCollection<string> PersonnelName
    {
        get { return _PersonnelName; }
        set
        {
            if (value != _PersonnelName)
            {
                _PersonnelName = value;
                OnPropertyChanged("PersonnelName");
            }
        }
    } 
    #endregion

    #region Constructor
    public VehicalModel(string VehicalNo, string Model, DateTime? ManufacturingDate, string IUNo, ObservableCollection<string> PersonnelName)
    {
        this.VehicalNo = VehicalNo;
        this.Model = Model;
        this.ManufacturingDate = ManufacturingDate;
        this.IUNo = IUNo;
        this.PersonnelName = PersonnelName;
        if(!(Validate_VehicalNo() && Validate_Model() && Validate_ManufacturingDate() && Validate_IUNo()))
            throw(new Exception("Invalid Data"));
    }
    public VehicalModel()
    {
        VehicalNo = null;
        Model = null;
        ManufacturingDate = null;
        IUNo = null;
        PersonnelName = new ObservableCollection<string>();

    } 
    #endregion

    #region Methods

    #region Validate Methods

    public bool Validate_VehicalNo()
    {
        if (VehicalNo == null)
            return false;
        if (matchRE(VehicalNo,"[A-Zz-z][A-Zz-z0-9]{6}"))
            return true;
        else
            return false;
    }
    public bool Validate_Model()
    {
        if (Model == null)
            return false;
        if(Model!=null || Model.Length==0)
            return true;
        else
            return false;
    }
    public bool Validate_ManufacturingDate()
    {
        if (ManufacturingDate == null)
            return false;
        return true;
    }
    public bool Validate_IUNo()
    {
        if (IUNo == null || Model.Length==0)
            return false;
        if(matchRE(IUNo,"[0-9]{10}"))
            return true;
        else
            return false;
    }
    public bool Validate_PersonnelName()
    {
        if (PersonnelName == null)
            return false;
        //if(matchRE(PersonnelName,"[A-Za-z]+"))
        //    return true;
        else
            return false;

    }  

    public bool matchRE(string stringToMatch, string regularExpression)
    {
        Regex regex = new Regex(@regularExpression);
        Match match = regex.Match(stringToMatch);

        if(match.Success)
            return(true);
        else
            return(false);
    }
    #endregion

    #endregion
}
}

2 个答案:

答案 0 :(得分:0)

您无法绑定到某个活动,FrameworkElement.Loaded是一个事件。

您可以做的是创建ICommand(而不是事件处理程序)并查看某种事件到命令解决方案。


在阅读了您尝试执行的操作实际执行的操作后,我实际上建议您将集合绑定到ItemsSource属性,而不是在加载控件后尝试设置项源。如果您只想将其绑定一次,则可以始终使用BindingMode.OneTime设置。

<ComboBox ItemsSource="{Binding Path=PersonnelName}" />

答案 1 :(得分:0)

您无法将方法绑定到ViewModel中的Loaded事件。在后面的代码中移动事件处理程序并将您的XAML更改为:

<ComboBox Name="Personnel_Combo" Loaded="Personnel_Combo_Loaded" 
          HorizontalAlignment="Left" Width="116"/>

同样访问ViewModel中的UI元素首先是违反MVVM。


此外,在加载处理程序中,您正在设置ItemSource,因此请删除Loaded处理程序并直接绑定到XAML中的属性:

<ComboBox Name="Personnel_Combo" ItemsSource="{Binding PersonnelName}" 
          HorizontalAlignment="Left" Width="116"/>

如果您打算在事件处理程序中执行更多操作而不是设置ItemsSource,则必须在ViewModel中创建ICommand并使用交互触发器绑定到事件。请查看答案here,以便开始此操作。