DatagridTemplateColumn / Combobox未显示ItemsSource

时间:2014-07-27 23:14:20

标签: wpf datagrid combobox

在我的ViewModel中,我有一个Unit类的BindableCollection(使用Caliburn Micro)。 Unit类有两个属性,UnitID(int)和UnitName(string)。此集合是ComboBox的ItemsSource,它是DatagridTemplateColumn。我知道正在填充BindableCollection,但是单元格没有显示在ComboBox中,UnitNames也没有显示在" From Unit"和#34; To Unit"列。

以下是我的ViewModel的重要部分:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vulcan.Entities;
using Vulcan.Conduits;
using Caliburn.Micro;

namespace Vulcan.ViewModels
{
    public class EditUnitConversionsViewModel : Screen, IActiveContentViewModel
    {
        IUnitConversionConduit _conversionConduit;
        IUnitConduit _unitConduit;
        EventAggregator _eventAggregator;

        public BindableCollection<UnitConversion> UnitConversions { get; set; }
        public int SelectedConversionIndex { get; set; }

        public BindableCollection<Unit> Units { get; set; }
        public int SelectedFromUnitIndex { get; set; }
        public int SelectedToUnitIndex { get; set; }


        public EditUnitConversionsViewModel(IUnitConversionConduit conversionConduit, IUnitConduit unitConduit, EventAggregator agg)
        {
            _conversionConduit = conversionConduit;
            _unitConduit = unitConduit;
            _eventAggregator = agg;

            Units = new BindableCollection<Unit>(_unitConduit.GetUnits());
            UnitConversions = new BindableCollection<UnitConversion>(_conversionConduit.GetUnitConversions().Select(c => new UnitConversion(c, _unitConduit)));
        }

以下是Unit和UnitConversion类:

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

namespace Vulcan.Entities
{
    public class Unit
    {
        private int _unitID;
        public int UnitID
        {
            get { return _unitID; }
            set { _unitID = value; }
        }

        private string _unitName;
        public string UnitName
        {
            get { return _unitName; }
            set { _unitName = value; }
        }

        public Unit(int unitID, string unitName)
        {
            this.UnitID = unitID;
            this.UnitName = unitName;
        }
    }
}

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

namespace Vulcan.Entities
{
    public class UnitConversion
    {
        public int UnitConversionID { get; set; }

        public int FromUnitID { get; set; }
        public int ToUnitID { get; set; }

        //Ex. meters to millimeters: ConversionFactor = 1000, Offset = 0
        // Fahrenheit to Celsius: Conversion Factor = 0.5556, Offset = 17.7778
        public double ConversionFactor { get; set; }
        public double Offset { get; set; }

        public UnitConversion(int id, int fromUnit, int toUnit, double conversionFactor, double offset)
        {
            this.UnitConversionID = id;
            this.FromUnitID = fromUnit;
            this.ToUnitID = toUnit;
            this.ConversionFactor = conversionFactor;
            this.Offset = offset;
        }
    }
}

这是我目前的XAML:

<DataGrid.Columns>
    <DataGridTemplateColumn Header="From Unit" Width="*">
        <DataGridTemplateColumn.CellTemplate>
             <DataTemplate>
                  <ComboBox DisplayMemberPath="UnitName" SelectedValuePath="UnitID" 
                                            SelectedValue="{Binding Path=FromUnitID}"
                                            ItemsSource="{Binding Path=Units}" />
             </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTemplateColumn Header="To Unit" Width="*">
         <DataGridTemplateColumn.CellTemplate>
              <DataTemplate>
                  <ComboBox DisplayMemberPath="UnitName" SelectedValuePath="UnitID" 
                                            SelectedValue="{Binding Path=ToUnitID}"
                                            ItemsSource="{Binding Units}" />
               </DataTemplate>
         </DataGridTemplateColumn.CellTemplate>
     </DataGridTemplateColumn>
     <DataGridTextColumn Header="Conversion Factor" Binding="{Binding ConversionFactor}" Width="*" />
     <DataGridTextColumn Header="Offset" Binding="{Binding Offset}" Width="*" />

以下是DataGrid的屏幕截图: enter image description here

我需要在XAML中更改什么才能让Unit集合显示在组合框中?

2 个答案:

答案 0 :(得分:1)

我根据你的描述写了一些代码。

  <DataGrid x:Name="dg" ItemsSource="{Binding LstUnitC}" AutoGenerateColumns="False" >
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="From Unit" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox DisplayMemberPath="UnitName"  ItemsSource="{Binding Units}"
                                            />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

            </DataGrid.Columns>
        </DataGrid>

<强>背景

private List<UnitConversions> lstunitc;

        public List<UnitConversions> LstUnitC
        {
            get { return lstunitc; }
            set { lstunitc = value; }
        }


        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            List<Units> lstunit = new List<Units>();
            Units u = new Units();
            u.UnitName = "asd";

            lstunit.Add(u);
            u = new Units();
            u.UnitName = "asd1";

            lstunit.Add(u);


            LstUnitC = new List<UnitConversions>();
            UnitConversions uc = new UnitConversions();
            uc.Units = lstunit;
            LstUnitC.Add(uc);
            dg.ItemsSource = LstUnitC;
        }



    }

    public class Units
    {
        private string _UnitName;

        public string UnitName
        {
            get { return _UnitName; }
            set { _UnitName = value; }
        }

    }

    public class UnitConversions
    {
        private List<Units> units;

        public List<Units> Units
        {
            get { return units; }
            set { units = value; }
        }


    }

确保Units是一个列表,该列表是UnitConversion Class的成员。

答案 1 :(得分:1)

您确实可以访问DataGrid行中的Units集合。您只能访问UnitConversion类中的属性(即行所代表的类),以绑定到您需要执行以下操作的集合:

<ComboBox DisplayMemberPath="UnitName" 
          SelectedValuePath="UnitID" 
          SelectedValue="{Binding Path=FromUnitID}"
          ItemsSource="{Binding DataContext.Units, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}}" />