将项添加到数据网格中的组合框

时间:2016-04-13 16:09:15

标签: c# wpf datagrid

我有一个由组合框和文本框组成的简单数据网格。最后一个名为" cost"是一个文本框,是所选组合框组合的结果。这是xaml:

<DataGrid AutoGenerateColumns="False" x:Name="myGrid"  ItemsSource="{Binding Path=Routes,UpdateSourceTrigger=PropertyChanged}" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="Sequenza N°"/>
            <DataGridComboBoxColumn Width="100" x:Name="Product" 
        SelectedValueBinding="{Binding Product, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="Product" 
        DisplayMemberPath="{Binding Product}"  >
            </DataGridComboBoxColumn>  
            <DataGridComboBoxColumn Width="100" x:Name="Quality" 
        SelectedValueBinding="{Binding Quality, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="Cestello" 
        DisplayMemberPath="{Binding Quality}"  >
            </DataGridComboBoxColumn>
            <DataGridTextColumn Binding="{Binding Cost, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="Cost"/>
        </DataGrid.Columns>
    </DataGrid>

后面的代码是一个名为Routing的类,它定义了一个值列表,并使用propertychanged来更改Cost文本框值。

namespace weblego
{
public partial class Prepare : Window
{

    public List<Routing> Routes { get; set; }
    public Prepare()
    {
        InitializeComponent();
        Routes = new List<Routing>()
            {
            new Routing() { Product = "triangolo"}
            };
        string[] stazioni = { "stazione1", "stazione2", "stazione3" };
        string[] qualita = { "low", "medium", "high" };
        Product.ItemsSource = stazioni;
        Quality.ItemsSource = qualita;
        myGrid.ItemsSource = Routes;
    }
}
public class Routing : INotifyPropertyChanged
{
    private string product;
    public string Product
    {
        get { return product; }
        set
        {
            if (product != value)
            {
                product = value;
                OnPropertyChanged(value);
            }
        }
    }

    private string quality;
    public string Quality
    {
        get { return quality; }
        set
        {
            if (quality != value)
            {
                quality = value;
                UpdateCost();
                OnPropertyChanged(value);
            }
        }
    }


    private double cost;
    public double Cost
    {
        get { return cost; }
        set
        {
            if (cost != value)
            {
                cost = value;
                OnPropertyChanged("Cost");
            }
        }
    }

    public void UpdateCost()
    {
        double qualityMultiple = 1;
        switch (Quality)
        {
            case "high":
                qualityMultiple = 1.5;
                break;
            case "medium":
                qualityMultiple = 1;
                break;
            case "low":
                qualityMultiple = 0.5;
                break;
        }

        switch (Product)
        {
            case "stazione1":
                Cost = 10 * qualityMultiple;
                break;
            case "stazione2":
                Cost = 15 * qualityMultiple;
                break;
            case "stazione3":
                Cost = 12.5 * qualityMultiple;
                break;
        }

    }

    // Create the OnPropertyChanged method to raise the event
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}
}

一切正常,但我做了一个计划错误。 xaml中的最后一个字段称为&#34; Cost&#34;应该是一个组合框,而不是文本框,并且必须填充来自数据库的值。所以XAML应该成为:

<DataGridComboBoxColumn Width="100" x:Name="Cost" 
        SelectedValueBinding="{Binding Cost, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="Cost" 
        DisplayMemberPath="{Binding Cost}" />

后面的代码应该让我以某种方式更改Cost元素上的itemsource。

    private List<string> cost;
    public List<string> Cost
    {
        get { return cost; }
        set
        {
            if (cost != value)
            {
                cost = value;
                OnPropertyChanged("Cost");
            }
        }
    }
        public void UpdateCost()
    {
        if(Quality=="high" && Product == "stazione1")
        {
            Cost.Add("abc");
            Cost.Add("def");
        }

    }

但是当我这样做时,我得到一个System.NullReferenceException&#39;说成本为空。如何根据行中其他字段中的选定值更改最后一个组合框的项目源?

0 个答案:

没有答案