简体   繁体   English

DatagridComboBoxColumn不显示值

[英]DatagridComboBoxColumn not displaying value

I'm having some difficulty with the comboboxcolumn within a data grid, I want the value that's displayed and edited taken from the data object used by the rest of the data grid and the values for the drop down list taken from another data object. 我在使用数据网格内的comboboxcolumn遇到一些困难,我希望显示和编辑的值取自其余数据网格使用的数据对象,而下拉列表的值取自另一个数据对象。 I've got the combobox dropdown list to work but I'm having difficulty getting it to show the value from the data grid row context. 我有combobox dropdown列表可以使用,但是我很难使它显示数据网格行上下文中的值。

Here's my layout 这是我的布局

    <StackPanel Orientation="Horizontal">
        <Label>Sheet date:</Label>
        <DatePicker/>     
    </StackPanel>
    <DataGrid x:Name="gridTimesheets" Height="205" Margin="4, 4, 4, 0" AutoGenerateColumns="False" >
        <DataGrid.Columns>
            <DataGridTextColumn IsReadOnly="True" Header="No." Width="40" Binding="{Binding ItemNumber }"></DataGridTextColumn>

            <DataGridComboBoxColumn Width="120" Header="WorksorderID" x:Name="worksorderColumn"                       
            SelectedValueBinding="{Binding Path=WorksorderID}"  SelectedValuePath="WorksorderID"
             DisplayMemberPath="WONA">                   
            </DataGridComboBoxColumn>

            <DataGridTextColumn Header="Time On" Width="60" Binding="{Binding TimeOn}"></DataGridTextColumn>
            <DataGridTextColumn Header="Time Off" Width="60" Binding="{Binding TimeOff}"></DataGridTextColumn>
            <DataGridTextColumn IsReadOnly="True" Header="Duration" Width="65" Binding="{Binding Duration}"></DataGridTextColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="Delete" x:Name="btnDelete"
                             Click="btnDelete_Click"></Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate> 
            </DataGridTemplateColumn>

        </DataGrid.Columns>
    </DataGrid>
    <Button Content="Add" Margin="4, 4, 4, 0" Click="btnAdd_Click"></Button>
    <Button Content="Export" Margin="4, 4, 4, 4" ></Button>
</StackPanel>  

Here's my code 这是我的代码

namespace Timesheet
{

public partial class MainWindow : Window
{

    TimesheetDatabase.TimesheetDB timesheetdb = new TimesheetDatabase.TimesheetDB();
    EFACSDatabase.EFACSDB efacsdb = new EFACSDatabase.EFACSDB();
    public MainWindow()
    {
        InitializeComponent();
        gridTimesheets.ItemsSource = timesheetdb.GetTimesheetItems();
        worksorderColumn.ItemsSource = efacsdb.GetOrderItems();

    }

OrderItem OrderItem的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;

namespace EFACSDatabase
{
public class OrderItem : INotifyPropertyChanged
{
    private string orderid;
    public string OrderID
    {
        get { return orderid; }
        set
        {
            orderid = value;
            OnPropertyChanged(new PropertyChangedEventArgs("OrderID"));
        }
    }

    private string wona;
    public string WONA
    {
        get { return wona; }
        set
        {
            wona = value;
            OnPropertyChanged(new PropertyChangedEventArgs("WONA"));
        }
    }

    private string partid;

    public string PartID
    {
        get { return partid; }
        set
        {
            partid = value;
            OnPropertyChanged(new PropertyChangedEventArgs("PartID"));
        }
    }

    private string customer;
    public string Customer
    {
        get { return customer; }
        set
        {
            customer = value;
            OnPropertyChanged(new PropertyChangedEventArgs("Customer"));
        }
    }

    private string description;

    public string Description
    {
        get { return description; }
        set
        {
            description = value;
            OnPropertyChanged(new PropertyChangedEventArgs("Description"));
        }

    }

     public OrderItem(string orderid, string wona, string partid, string customer, string description)
    {
        OrderID = orderid;
        WONA = wona;
        PartID = partid;
        Customer = customer;
        Description = description;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, e);
    }
}
}

TimesheetItem TimesheetItem

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace TimesheetDatabase
{
public class TimesheetItem : INotifyPropertyChanged
{

    private int timesheetitemid;
    public int TimesheetItemID
    {
        get { return timesheetitemid; }
        set
        {
            timesheetitemid = value;
            OnPropertyChanged(new PropertyChangedEventArgs("TimesheetItemID"));
        }
    }

    private int timesheetid;
    public int TimesheetID
    {
        get { return timesheetid; }
        set
        {
            timesheetid = value;
            OnPropertyChanged(new PropertyChangedEventArgs("TimesheetID"));
        }
    }

    private int itemnumber;
    public int ItemNumber
    {
        get { return itemnumber; }
        set
        {
            itemnumber = value;
            OnPropertyChanged(new PropertyChangedEventArgs("ItemNumber"));
        }
    }



    private TimeSpan timeon;
    public TimeSpan TimeOn
    {
        get { return timeon; }
        set
        {
            timeon = value;
            OnPropertyChanged(new PropertyChangedEventArgs("TimeOn"));
        }
    }

    private TimeSpan timeoff;
    public TimeSpan TimeOff
    {
        get { return timeoff; }
        set
        {
            timeoff = value;
            OnPropertyChanged(new PropertyChangedEventArgs("TimeOff"));
        }
    }
    private String worksorderid;
    public String WorksorderID
    {
         get { return worksorderid; }
         set
         {
             worksorderid = value;
             OnPropertyChanged(new PropertyChangedEventArgs("WorksorderID"));
         }
     }


    private double duration;
    public double Duration
    {
        get { return duration; }
        set
        {
            duration = value;
            OnPropertyChanged(new PropertyChangedEventArgs("Duration"));
        }    
    }

    public TimesheetItem(int timesheetitemid, int timesheetid, int itemnumber, TimeSpan timeon, TimeSpan timeoff, String worksorderid, double duration)
    {
        TimesheetItemID = timesheetitemid;
        TimesheetID = timesheetid;
        ItemNumber = itemnumber;
        TimeOn = timeon;
        TimeOff = timeoff;
        WorksorderID = worksorderid;
        Duration = duration;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, e);
    }
}
}

I'm trying to get the combobox to display a value from GetTimesheetItems and allow the user to change that value from the drop down using values from GetOrderItems . 我试图让combobox显示从值GetTimesheetItems ,并允许用户以该值从下拉使用从值的变化GetOrderItems

I think this is what you need: 我认为这是您需要的:

<DataGridComboBoxColumn Width="120" 
                        Header="WorksorderID" 
                        x:Name="worksorderColumn"                       
                        SelectedValueBinding="{Binding Path=WorksorderID}"
                        SelectedValuePath="WONA"
                        DisplayMemberPath="WONA">                   
</DataGridComboBoxColumn>
  • The SelectedValueBinding is bound to the WorksorderID property of the TimesheetItem object, which is the item of the DataGrid . SelectedValueBinding绑定到TimesheetItem对象的WorksorderID属性,该对象是DataGrid的项。 This means that when you select one item from the combo, its value (determined by SelectedValuePath ) will be assigned to the WorksorderID property of the DataGrid item. 这意味着,当您从组合中选择一项时,其值(由SelectedValuePath确定)将分配给DataGrid项的WorksorderID属性。
  • The SelectedValuePath is set to the WONA property of the OrderItem object selected on the ComboBox . SelectedValuePath设置为在ComboBox上选择的OrderItem对象的WONA属性。 This indicates that the value of each item in the combo is determined by its WONA property, that according to you, corresponds to the WorksorderID... 这表明组合中每个项目的值由其WONA属性确定,根据您的说法,该值对应于WorksorderID ...
  • And similarly, the DisplayMemberPath is set to the WONA property of the OrderItem objects contained on the ComboBox drop-down list. 同样, DisplayMemberPath设置为ComboBox下拉列表中包含的OrderItem对象的WONA属性。 This just selects the property you wish to visualize inside the combo, for each item. 这只是为每个项目选择希望在组合中可视化的属性。

EDIT: If you have problems with the combos being empty in the initial load, then invert the order on which you're loading the items. 编辑:如果您在初始加载中组合为空时遇到问题,请反转加载项目的顺序。 I mean, first load the items of the ComboBoxColumn, and then the items of the DataGrid. 我的意思是,首先加载ComboBoxColumn的项,然后加载DataGrid的项。

public MainWindow()
{
    InitializeComponent();
    worksorderColumn.ItemsSource = efacsdb.GetOrderItems();
    gridTimesheets.ItemsSource = timesheetdb.GetTimesheetItems();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM