Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.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# Can';t获取组合框的SelectedItem或SelectedIndex_C#_Wpf_Mvvm - Fatal编程技术网

C# Can';t获取组合框的SelectedItem或SelectedIndex

C# Can';t获取组合框的SelectedItem或SelectedIndex,c#,wpf,mvvm,C#,Wpf,Mvvm,我开始爬墙了。我在一个组合框中填入了一系列项目,我试图找出哪些项目被选中。我还试图将组合框设置为自动选择列表中的第0项。但这也不行,我在GroupIndex和SelectedGroup上都遇到了绑定表达式路径错误。但是我不知道问题出在哪里 组合框Xaml: <ComboBox Name="GroupComboBox" SelectedIndex="{Binding Path=GroupIndex}" SelectedItem="{Binding Path=SelectedGrou

我开始爬墙了。我在一个组合框中填入了一系列项目,我试图找出哪些项目被选中。我还试图将组合框设置为自动选择列表中的第0项。但这也不行,我在GroupIndex和SelectedGroup上都遇到了绑定表达式路径错误。但是我不知道问题出在哪里

组合框Xaml:

<ComboBox     
Name="GroupComboBox"
SelectedIndex="{Binding Path=GroupIndex}"
SelectedItem="{Binding Path=SelectedGroup}"
DisplayMemberPath="Name"
SelectedValuePath ="Name"
ItemsSource="{Binding Path=Groups}"
Height="38"
HorizontalAlignment="Left"
Margin="159,115,0,0"
VerticalAlignment="Top"
Width="185"
FontSize="24" Text="Select a Group" IsEditable="False" IsReadOnly="False" />

下面是填充组合框的代码

    void webService_GroupListChanged(string response)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();

        IList<JSONGroup> listOfGroups = new List<JSONGroup>();

        listOfGroups = serializer.Deserialize<List<JSONGroup>>(response);

        Application.Current.Dispatcher.BeginInvoke((Action)(() =>
        {
            Groups = new ObservableCollection<Group>();

            foreach(JSONGroup group in listOfGroups)
            {
                Groups.Add(new Group(group.name, group.id));
            }

        }));
    }

    private int groupIndex;
    private int GroupIndex
    {
        get { return this.groupIndex; }
        set
        {
            if (this.groupIndex != value)
            {
                this.groupIndex = value;
                this.OnPropertyChanged("GroupIndex");
            }
        }
    }


    private Group selectedGroup;
    private Group SelectedGroup
    {
        get { return this.selectedGroup; }
        set
        {
            if (this.selectedGroup != value)
            {
                this.selectedGroup = value;
                this.OnPropertyChanged("SelectedGroup");
            }
        }
    }


    private ObservableCollection<Group> groups;
    public ObservableCollection<Group> Groups
    {
        get { return this.groups; }
        set
        {
            if (this.groups != value)
            {
                this.groups = value;
                this.OnPropertyChanged("Groups");
            }
        }
    }
void webService\u GroupListChanged(字符串响应)
{
JavaScriptSerializer serializer=新的JavaScriptSerializer();
IList listOfGroups=新列表();
listOfGroups=serializer.Deserialize(响应);
Application.Current.Dispatcher.BeginInvoke((操作)(()=>
{
组=新的可观察集合();
foreach(组列表中的JSONGroup组)
{
添加(新组(Group.name,Group.id));
}
}));
}
私有int-groupIndex;
私有整数组索引
{
获取{返回this.groupIndex;}
设置
{
if(this.groupIndex!=值)
{
this.groupIndex=值;
本.OnPropertyChanged(“GroupIndex”);
}
}
}
私有组selectedGroup;
私人组SelectedGroup
{
获取{返回this.selectedGroup;}
设置
{
if(this.selectedGroup!=值)
{
this.selectedGroup=值;
此.OnPropertyChanged(“SelectedGroup”);
}
}
}
私人可观察收集组;
公共收集组
{
获取{返回this.groups;}
设置
{
if(this.groups!=值)
{
这个组=值;
本协议项下的不动产变更(“集团”);
}
}
}
  • 要在绑定中使用,您的
    GroupIndex
    SelectedGroup
    属性必须是
    public
  • 不要同时设置
    SelectedItem
    SelectedIndex
  • 如果您希望设置
    SelectedItem
    ,则该项目(
    SelectedGroup
    )必须包含在
    组中

  • 您正在设置DataContext吗?如果是这样,该怎么办?视图(用户控件)的datacontext设置为上面代码所在的viewmodel。组合框中项目的实际列表的绑定可以工作,因此理论上其他两个绑定也应该可以工作。那么应该可以工作了。您只需要SelectedItem和SelectedIndex中的一个。很好,SelectedGroup也是如此。@Phil没有发现滚动条:-)