子数据网格中的c#wpf组合框列

时间:2018-01-18 17:10:54

标签: c# wpf combobox datagrid

我正在使用两个DataGrids的WPF应用程序 - 一个用于TimeCard对象,另一个用于时间卡行(“TcLine”)对象。 TimeCards和TcLines之间存在一对多的关系。使用SelectionChanged事件设置TimeCard数据网格,该事件使用相关的TcLine记录填充子数据网格。该事件工作正常但我想让这个TcLine数据网格包含一个DataGridComboBoxColumn,以允许用户从下拉列表中更改给定行的批准状态。到目前为止,我还没有能够使DataGridComboBoxColumn工作。这是XAML:

    <Grid>
    <DataGrid x:Name="TimeCardGrid" HorizontalAlignment="Left" 
              Height="100" Margin="62,30,0,0" VerticalAlignment="Top" 
              Width="454" AutoGenerateColumns="False" 
              SelectionChanged="TimeCardGrid_SelectionChanged">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Id" Binding="{Binding EmployeeId}" 
                                Width="75"></DataGridTextColumn>
            <DataGridTextColumn Header="Week Ending" 
                                Binding="{Binding WeekEnding}" 
                                Width="*"></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>
    <DataGrid x:Name="TcLineGrid" HorizontalAlignment="Left" Height="154" Margin="85,207,0,0" 
              VerticalAlignment="Top" Width="431" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Job Number" 
                                Binding="{Binding JobNumber}" Width="80"/>
            <DataGridTextColumn Header="Phase Code" 
                                Binding="{Binding PhaseCode}" Width="80"/>
            <DataGridTextColumn Header="Rate Factor" 
                                Binding="{Binding RateFactor}" Width="80"/>
            <DataGridComboBoxColumn Header="Mon Approval" x:Name="MondayApproval" Width="90" 
                                    SelectedItemBinding="{Binding MondayApproval, Mode=TwoWay}" 
                                    DisplayMemberPath="{Binding MondayApproval}"/>
            <DataGridTextColumn Header="Monday" Binding="{Binding MondayHours}" Width="*"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

以下是主窗口的代码:

    public partial class MainWindow : Window
{
    List<TimeCard> timeCards;
    List<string> approvalStatuses;
    TimeCard timeCard1;
    TimeCard timeCard2;

    public MainWindow()
    {
        InitializeComponent();
        //load timncard objects
        timeCard1 = new TimeCard(1, DateTime.Parse("1/5/2018"));
        timeCard1.TcLines.Add(new TcLine(DateTime.Parse("1/5/2018"), "941119", "009-150-", 1.0));
        timeCard1.TcLines.Add(new TcLine(DateTime.Parse("1/5/2018"), "941119", "009-150-", 1.5));
        timeCard1.TcLines.Add(new TcLine(DateTime.Parse("1/5/2018"), "941119", "009-800-", 0.0));
        timeCard2 = new TimeCard(2, DateTime.Parse("1/5/2018"));
        timeCard2.TcLines.Add(new TcLine(DateTime.Parse("1/5/2018"), "971290", "009-151-", 1.0));
        timeCard2.TcLines.Add(new TcLine(DateTime.Parse("1/5/2018"), "971290", "009-151-", 1.5));
        timeCard2.TcLines.Add(new TcLine(DateTime.Parse("1/5/2018"), "971290", "009-800-", 0.0));
        timeCards = new List<TimeCard> { timeCard1, timeCard2 };
        TimeCardGrid.ItemsSource = timeCards;
        //load approval status combobox options
        approvalStatuses = new List<string>
        {
            "Approved",
            "Unapproved"
        };
    }

    private void TimeCardGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        TcLineGrid.Items.Clear();
        object item = TimeCardGrid.SelectedItem;
        int id = int.Parse((TimeCardGrid.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text);
        DateTime weekEnding = DateTime.Parse((TimeCardGrid.SelectedCells[1].Column.GetCellContent(item) as TextBlock).Text);
        var timeCard = timeCards.Where(t => t.EmployeeId == id && t.WeekEnding == weekEnding).FirstOrDefault();
        foreach (var line in timeCard.TcLines)
        {
            TcLineGrid.Items.Add(line);
        }
        //set combobox column item source 
        MondayApproval.ItemsSource = approvalStatuses;
    }

这些是我的模特:

        public class TimeCard
    {
        public TimeCard() { TcLines = new List<TcLine>(); }

        public TimeCard(int employeeId, DateTime weekEnding)
        {
            EmployeeId = employeeId;
            WeekEnding = weekEnding;
            TcLines = new List<TcLine>();
        }

        public int EmployeeId { get; set; }
        public DateTime WeekEnding { get; set; }
        public List<TcLine> TcLines { get; set; }
        public double PerDiem { get; set; }
    }

    public class TcLine
    {
        public TcLine(DateTime weekEnding, string jobNumber, string phaseCode, double rateFactor)
        {
            JobNumber = jobNumber;
            PhaseCode = phaseCode;
            RateFactor = rateFactor;
            MondayHours = 0.0;
            MondayApproval = "A";
        }
        public string JobNumber { get; set; }
        public string PhaseCode { get; set; }
        public double RateFactor { get; set; }
        public double MondayHours { get; set; }
        public string MondayApproval { get; set; }
    }

}

我尝试了没有Time卡对象和网格的版本,而DataGridComboBoxColumn的XAML运行正常,所以我猜这个问题与我的SelectionChanged事件有关。我尝试将此列的ItemSource属性的赋值从主窗口构造函数移动到选择更改的方法,但这不起作用。有人有过类似的问题吗?对于冗长和TIA

抱歉

1 个答案:

答案 0 :(得分:0)

直接在SelectionChanged方法中设置项目可防止DataGrid项目可编辑。改为设置ItemsSource。你确实可以用单行代码来做到这一点:

    private void TimeCardGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
         TcLineGrid.ItemsSource = (TimeCardGrid.SelectedItem as TimeCard)?.TcLines;
    }

您可以在MainWindow的构造函数中保存的ComboBox的ItemsSource。