检查datagrid是否具有值

时间:2018-11-22 10:41:36

标签: c# wpf data-binding

我正在研究一个包含WPF的C#项目。我想知道,是否可以通过某种方式检查我的数据网格是否包含某些元素。

例如, 我有一个组合框,其itemsSource是一些对象列表。现在,当用户从组合框中选择一个项目并按下按钮Before click

在数据网格下方(在同一窗口中),该项目将显示。After clicking

我想禁止用户多次选择同一项目,例如,将MessageBox放入错误消息。我该怎么办?

代码

此窗口:

public partial class AvioWindowAddNEdit : Window
{
    Avio avio;
    public enum Stage { ADD, EDIT};
    Stage stage;

    public AvioWindowAddNEdit(Avio avio, Stage stage = Stage.ADD)
    {
        InitializeComponent();
        this.avio= avio;
        this.stage= stage;

        textboxCode.DataContext = avio;



        comboboxListofFlights.ItemsSource = Aplikacija.Instance.Flights;
        comboboxListofFlights.DataContext = avio;


        datagridListofFlights.ItemsSource = avio.ListofFlights;




        datagridListofFlights.ColumnWidth = new DataGridLength(1, DataGridLengthUnitType.Auto);

        if (stage== Stage.EDIT)
        {
            textboxCode.IsEnabled = false;
        }
    }
}

将所选项目添加到数据网格的按钮:

private void btnAddFlight_Click(object sender, RoutedEventArgs e)
    {

        avio.ListOfFlights.Add(comboboxListOfFlights.SelectedItem as Flight);

    }

用于加载我的所有数据的单一类:

class Aplication
{
    public ObservableCollection<User> Users { get; set; }
    public ObservableCollection<Airport> Airports { get; set; }
    public ObservableCollection<Flight> Flights{ get; set; }
    public ObservableCollection<Avio> Avios { get; set; }

    public string LoggedInUser { get; set; }

    private static Aplication instance = new Aplication();

    public static Aplication Instance
    {
        get
        {
            return instance;
        }
    }

    private Aplication()
    {
        Users= new ObservableCollection<User>();
        Airports = new ObservableCollection<Airport>();
        Flights = new ObservableCollection<Flight>();
        Avios= new ObservableCollection<Avio>();
        FillInData(); //method where I filled in all of these ObservableCollections
    }
}

我的课:

public class Avio : ObservableObject, ICloneable
{
    //observableobject is an object where I implemented INotifyPropertyChanged
    private string code;

    public string Code
    {
        get { return code; }
        set { code= value; OnPropertyChanged("Code"); }
    }

    private ObservableCollection<Flight> listOfFlights;

    public ObservableCollection<Flight> ListOfFlights
    {
        get { return listOfFlights; }
        set { listOfFlights= value; OnPropertyChanged("ListOfFlights"); }
    }


    private bool active;

    public bool Active
    {
        get { return active; }
        set { active= value; OnPropertyChanged("Active"); }
    }

    public Avio()
    {
        active= true;
        ListOfFlights = new ObservableCollection<Flight>();
    }
    public Avio(string code)
    {
        active= true;
        ListOfFlights = new ObservableCollection<Flight>();
        Code= code;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以将ObservableCollection用作DataGrid的ItemsSource。这样,您将始终可以通过代码轻松访问数据。 查看this教程作为起点(使用ListBox代替DataGrid,但它很容易适应DataGrid)。