WPF如何在listview中使用不同的itemsource设置绑定组合框的选择项

时间:2014-12-01 16:09:43

标签: c# wpf

您好我在ListView项目上有一个ComboBox,ComboBox使用一个ObservableCollection而ListView使用另一个。如何将ComboBox的selecteditem设置为listview项目中列的值?

ListView使用GridView定义如下:

        <GridView x:Key="manage_calls_gridView">
        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding MCName}" />
        <GridViewColumn Header="Allocated To" DisplayMemberBinding="{Binding MCPostCode}" />
        <GridViewColumn Header="Post Code" Width="180" CellTemplate="{StaticResource manage_calls_pcode}" />
    </GridView>

使用DataTemplate中定义的ComboBox如下:

        <DataTemplate x:Key="manage_calls_pcode">
        <ComboBox Width="180" DropDownClosed="mc_pcode_DropDownClosed" DataContext="{Binding DataContext,RelativeSource={RelativeSource AncestorType={x:Type ListView}}}" 
                  ItemsSource="{Binding Path=LookUpCollection_6}" 
                  DisplayMemberPath="Desc"
                  />
    </DataTemplate>

这是ComboBox用于其ObservableCollection(LookUpCollection_6)的类

        public class LookUp
    {
        public string Id { get; set; }
        public string Desc { get; set; }
    }

这是ListView,包含ComboBox,用于其ObservableCollection(SalesAgentList)

        public class SalesAgentRec
    {
        public string MCName { get; set; }
        public string MCPostCode { get; set; }
        public string MCSource { get; set; }
        public string MCUid { get; set; }
        public string MCRecs { get; set; }
    }

我需要将ComboBox的选定项目/值设置为MCPostCode的值。

提前致谢,

史蒂夫。

3 个答案:

答案 0 :(得分:2)

在您的班级/数据模型中执行此操作 并删除datacontext

public class SalesAgentRec
    {
        public string MCName { get; set; }
        public string MCPostCode { get; set; }
        public string MCSource { get; set; }
        public string MCUid { get; set; }
        public string MCRecs { get; set; }
        public List<LookUpCollection_6> { get; set; }
    }

SelecteIndex =&#34; {Binding Path = MCPostCode}&#34;

对于像这样的简单ID描述使用词典。这两个属性是Key和Value。

答案 1 :(得分:0)

有什么理由说这不起作用吗?

  <DataTemplate x:Key="manage_calls_pcode">
    <ComboBox Width="180" DropDownClosed="mc_pcode_DropDownClosed"
      ItemsSource="{Binding DataContext.LookUpCollection_6 ,RelativeSource={RelativeSource AncestorType={x:Type ListView}}}"
      SelectedItem="{Binding MCPostCode}" 
      DisplayMemberPath="Desc" />
</DataTemplate>

而不是将ComboBox.DataContext设置为父ListView - 现在直接设置ItemsSoruce,允许您访问行的“MCPostCode”和#m;值。除非我错过了什么?

答案 2 :(得分:0)

这是对我有用的最终解决方案。

ComboBox的Changed DataTemplate:

        <DataTemplate x:Key="manage_calls_pcode">
        <ComboBox Width="90" SelectedIndex="{Binding Path=MCPostCodeX}" DropDownClosed="mc_pcode_DropDownClosed" 
                  ItemsSource="{Binding Path=MCCodes}" 
                  DisplayMemberPath="Value" 
                  Tag="{Binding Path=MCSIndex}"
                  />
    </DataTemplate>

对ComboBox的ItemSource使用Dictionary的更改:

public Dictionary<int, string> PCodeList = new Dictionary<int,string>();

用于加载词典的方法

        ArrayList lkl = serl.ListLookupsC(0);
        if (lkl.Count > 1)
        {
            LookUpCollection_6.Clear();
            LookUpCollection_7.Clear();
            for (int x = 0; x < lkl.Count; x++)
            {
                string[] strLData = (string[])lkl[x];
                PCodeList.Add(x, strLData[1]);
            }
        }

对SalesAgentRec类的更改

        public class SalesAgentRec
    {
        public string MCName { get; set; }
        public string MCPostCode { get; set; }
        public string MCSource { get; set; }
        public string MCUid { get; set; }
        public string MCRecs { get; set; }
        public Dictionary<int, string> MCCodes { get; set; }
        public int MCPostCodeX { get; set; }
        public string MCSIndex { get; set; }
    }

加载SalesAgentList集合的方法

    private void GetSalesAgents()
    {
        this.Cursor = Cursors.Wait;
        SerUsers seru = new SerUsers();
        ArrayList al = seru.GetTeleSales();
        _SalesAgentList.Clear();
        if (al.Count == 0)
        {
            this.Cursor = Cursors.Arrow;
            return;
        }
        for (int x = 0; x < al.Count; x++)
        {
            string[] strData = (string[])al[x];
            int nIndex = -1;
            // Use LINQ to find the key for the value
            if (strData[2].Length == 2)
            {
                var item = (from d in PCodeList
                            where d.Value.Substring(0, 2) == strData[2]
                            select d.Key).FirstOrDefault();

                nIndex = (int)item;
            }
            _SalesAgentList.Add(new SalesAgentRec{
                MCUid = strData[0],
                MCName = strData[1],
                MCPostCode = strData[2],
                MCCodes = PCodeList,
                MCPostCodeX = nIndex,
                MCSIndex = x.ToString()
            });
        }
        this.manage_calls_listView.View = this.manage_calls_listView.FindResource("manage_calls_gridView") as ViewBase;
        this.Cursor = Cursors.Arrow;
    }

上述代码尚未针对性能或错误处理进行优化,纯粹用于测试所提供的答案。由于该方法中SQL查询的性质,serl.ListLookupC(0)返回的值将是唯一的。

再次感谢所有花时间回复的人。