Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF如何在具有不同itemsource的listview中设置绑定组合框的selecteditem_C#_Wpf - Fatal编程技术网

C# WPF如何在具有不同itemsource的listview中设置绑定组合框的selecteditem

C# WPF如何在具有不同itemsource的listview中设置绑定组合框的selecteditem,c#,wpf,C#,Wpf,嗨,我在ListView项上有一个组合框,组合框使用一个ObservableCollection,ListView使用另一个。如何将组合框的selecteditem设置为listview项中某列的值 ListView使用的GridView定义如下: <GridView x:Key="manage_calls_gridView"> <GridViewColumn Header="Name" DisplayMemberBinding="{Bindin

嗨,我在ListView项上有一个组合框,组合框使用一个ObservableCollection,ListView使用另一个。如何将组合框的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 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>
这是包含组合框的ListView用于其ObservableCollection(SalesAgentList)的类

我需要将组合框的选定项/值设置为MCPostCode的值

提前感谢,


史蒂夫。

这有什么理由不起作用吗

  <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,而是直接设置ItemsRouce,允许您访问行的“MCPostCode”值。除非我遗漏了什么?

在你的课堂/数据模型中这样做
并删除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; }
    }
公共类SalesAgentRec
{
公共字符串MCName{get;set;}
公共字符串MCPostCode{get;set;}
公共字符串MCSource{get;set;}
公共字符串MCUid{get;set;}
公共字符串MCRecs{get;set;}
公共列表{get;set;}
}
SelecteIndex=“{Binding Path=MCPostCode}”


对于这样一个简单的ID描述,请使用字典。这两个属性是键和值。

这里是对我有效的最终解决方案

组合框的已更改数据模板:

        <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>
public Dictionary<int, string> PCodeList = new Dictionary<int,string>();

对组合框的ItemSource使用字典的更改:

        <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>
public Dictionary<int, string> PCodeList = new Dictionary<int,string>();
public Dictionary PCodeList=new Dictionary();
用于加载字典的方法

        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]);
            }
        }
ArrayList lkl=serl.ListLookupsC(0);
如果(lkl.Count>1)
{
LookUpCollection_6.Clear();
LookUpCollection_7.Clear();
对于(int x=0;x
对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; }
    }
公共类SalesAgentRec
{
公共字符串MCName{get;set;}
公共字符串MCPostCode{get;set;}
公共字符串MCSource{get;set;}
公共字符串MCUid{get;set;}
公共字符串MCRecs{get;set;}
公共字典MCCodes{get;set;}
公共整数MCPostCodeX{get;set;}
公共字符串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;
    }
private void GetSalesAgents()
{
this.Cursor=Cursors.Wait;
SerUsers seru=新的SerUsers();
ArrayList al=seru.GetTeleSales();
_SalesAgentList.Clear();
如果(al.Count==0)
{
this.Cursor=Cursors.Arrow;
返回;
}
对于(int x=0;x
上述代码尚未针对性能或错误处理进行优化,仅用于测试提供的答案。由于该方法中SQL查询的性质,serl.ListLookupC(0)返回的值将是唯一的


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

嗨,布拉姆,我接受了你的回复,因为这对我走上正轨最有帮助。谢谢你花时间回答。嗨,奥利特,谢谢你花时间回答我的问题。你的解决方案在我的情况下不起作用。不用担心@BURM有最好的方法,将视图所需的数据放入模型中。