【问题标题】:WPF How to Set selecteditem of bound combobox in a listview with different itemsourceWPF如何在具有不同项目源的列表视图中设置绑定组合框的选定项目
【发布时间】:2014-12-01 16:09:42
【问题描述】:

嗨,我在 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; }
    }

这是包含 ComboBox 的 ListView 用于其 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 的值。

提前致谢,

史蒂夫。

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    在您的类/数据模型中执行此操作
    并删除数据上下文

    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="{绑定路径=MCPostCode}"

    对于像这样的简单 ID Desc,请使用字典。这两个属性是 Key 和 Value。

    【讨论】:

    • 嗨,Blam,我已接受您的回复作为答案,因为它对让我走上正轨最有帮助。感谢您抽出宝贵时间回复。
    【解决方案2】:

    这有什么不可行的原因吗?

      <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' 值。除非我错过了什么?

    【讨论】:

    • 您好 Olitee,感谢您抽出宝贵时间回答我的问题。您的解决方案在我的情况下不起作用。
    • 不用担心。 @Blam 有最好的方法,将视图需要的数据放入模型中。
    【解决方案3】:

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

    组合框的更改数据模板:

            <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) 返回的值将是唯一的。

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

    【讨论】:

      猜你喜欢
      • 2010-12-31
      • 1970-01-01
      • 2014-05-23
      • 2016-08-10
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      • 2015-03-18
      • 2013-01-14
      相关资源
      最近更新 更多