Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.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# 多列组合框中的选定值_C#_Wpf_Xaml_Combobox - Fatal编程技术网

C# 多列组合框中的选定值

C# 多列组合框中的选定值,c#,wpf,xaml,combobox,C#,Wpf,Xaml,Combobox,我的数据库中有一个名为Groups的表,如下所示: Groups |--GroupID |--GroupName |--ParentID |--NatureOfGroupID |--EffectID 以下是上表的关系图: 我在窗口中有一个组合框,其中有两列。以下是xaml: <ComboBox ItemsSource="{Binding DataContext.GroupNamesWithCorrespondingEffects, RelativeSource={R

我的数据库中有一个名为Groups的表,如下所示:

Groups
  |--GroupID
  |--GroupName
  |--ParentID
  |--NatureOfGroupID
  |--EffectID
以下是上表的关系图:

我在窗口中有一个组合框,其中有两列。以下是xaml:

<ComboBox ItemsSource="{Binding DataContext.GroupNamesWithCorrespondingEffects, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}"
          SelectedValue="{Binding ParentID}" SelectedValuePath="GroupID">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding GroupName}" Width="200" />
                <TextBlock Text="{Binding CorrespondingEffect}" />
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
下面是groupsViewModel.cs的代码

public class GroupsViewModel : MainViewModel
{
    public GroupsViewModel()
    {

        using (Entities db = new Entities())
        {
            GroupNamesWithCorrespondingEffects = (from g in db.Groups
                                                  select new GroupNameWithCorrespondingEffect
                                                  {
                                                      GroupName = g.GroupName,
                                                      CorrespondingEffect = g.Master_Effects.Effect
                                                  }).ToList();

            NaturesOfGroup = (from m in db.Master_NatureOfGroup
                              select m).ToList();
        }

        SaveChangesCommand = new RelayCommand(SaveGroup);

        CurrentGroup = new Group();
    }

    private Group _currentGroup;
    public Group CurrentGroup
    {
        get
        {
            return _currentGroup;
        }

        set
        {
            _currentGroup = value;
            OnPropertyChanged("CurrentGroup");
        }
    }

    private List<GroupNameWithCorrespondingEffect> _groupNamesWithCorrespondingEffects;
    public List<GroupNameWithCorrespondingEffect> GroupNamesWithCorrespondingEffects
    {
        get
        {
            return _groupNamesWithCorrespondingEffects;
        }
        set
        {
            _groupNamesWithCorrespondingEffects = value;
            OnPropertyChanged("GroupNamesWithCorrespondingEffects");
        }
    }

    private List<Master_NatureOfGroup> _naturesOfGroup;
    public List<Master_NatureOfGroup> NaturesOfGroup
    {
        get
        {
            return _naturesOfGroup;
        }
        set
        {
            _naturesOfGroup = value;
            OnPropertyChanged("NaturesOfGroup");
        }
    }

    public ICommand SaveChangesCommand { get; set; }

    private void SaveGroup(object obj)
    {
        Group cGroup = new Group()
        {
            GroupName = CurrentGroup.GroupName,
            **ParentID = CurrentGroup.ParentID,**
            NatureOfGroupID = CurrentGroup.NatureOfGroupID,
            EffectID = CurrentGroup.EffectID
        };

        using (Entities db = new Entities())
        {
            db.Groups.Add(cGroup);
            db.SaveChanges();

            GroupNamesWithCorrespondingEffects = (from g in db.Groups
                                                  select new GroupNameWithCorrespondingEffect
                                                  {
                                                      GroupName = g.GroupName,
                                                      CorrespondingEffect = g.Master_Effects.Effect
                                                  }).ToList();

        }
    }
}
public类组viewmodel:MainViewModel
{
公共组viewmodel()
{
使用(Entities db=new Entities())
{
GroupNamesWithCorrespondingEffects=(来自db.Groups中的g
选择具有相应效果的新组名
{
GroupName=g.GroupName,
对应效应=g.Master\u效应
}).ToList();
NaturesOfGroup=(从db.Master中的m开始)\u NatureOfGroup
选择m.ToList();
}
SaveChangesCommand=newrelaycommand(存储组);
CurrentGroup=新组();
}
私有组_currentGroup;
公共组当前组
{
得到
{
返回当前组;
}
设置
{
_当前组=值;
OnPropertyChanged(“当前集团”);
}
}
具有相应效果的私有列表组名称;
具有相应效果的公共列表组名称
{
得到
{
返回具有相应效果的组名称;
}
设置
{
_具有相应效果的组名称=值;
OnPropertyChanged(“具有相应影响的组名称”);
}
}
私人名单(集团),;
公共列表组
{
得到
{
返回(u)组;;
}
设置
{
_性质组=值;
不动产变更(“集团性质”);
}
}
公共ICommand SaveChangesCommand{get;set;}
专用void存储组(对象obj)
{
组cGroup=新组()
{
GroupName=CurrentGroup.GroupName,
**ParentID=CurrentGroup.ParentID**
NatureOfGroupID=CurrentGroup.NatureOfGroupID,
EffectID=CurrentGroup.EffectID
};
使用(Entities db=new Entities())
{
db.Groups.Add(cGroup);
db.SaveChanges();
GroupNamesWithCorrespondingEffects=(来自db.Groups中的g
选择具有相应效果的新组名
{
GroupName=g.GroupName,
对应效应=g.Master\u效应
}).ToList();
}
}
}
当我调试并在标有**….**的行上放置断点时我总是得到CurrentGroup.ParentID=null

更新:

<Page.DataContext>
    <vm:GroupsViewModel />
</Page.DataContext>

<Grid DataContext="{Binding CurrentGroup}">

    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="0.6*" />
        <ColumnDefinition Width="0.6*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <TextBlock Grid.Row="1" Grid.Column="1" Text="Name" HorizontalAlignment="Left"/>
    <TextBlock Grid.Row="1" Grid.Column="1" Text=" : " HorizontalAlignment="Right"/>
    <TextBox Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" Text="{Binding GroupName}"/>

    <TextBlock Grid.Row="2" Grid.Column="1" Text="Under" HorizontalAlignment="Left"/>
    <TextBlock Grid.Row="2" Grid.Column="1" Text=" : " HorizontalAlignment="Right"/>
    <ComboBox x:Name="cbUnder" Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="2" 
              ItemsSource="{Binding DataContext.GroupNamesWithCorrespondingEffects, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}"
              SelectedValue="{Binding ParentID}" SelectedValuePath="GroupID">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding GroupName}" Width="200" />
                    <TextBlock Text="{Binding CorrespondingEffect}" />
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

    <TextBlock Grid.Row="3" Grid.Column="1" Text="Nature of Group" HorizontalAlignment="Left" Visibility="{Binding Visibility, ElementName=cbNature}"/>
    <TextBlock Grid.Row="3" Grid.Column="1" Text=" : " HorizontalAlignment="Right" Visibility="{Binding Visibility, ElementName=cbNature}"/>
    <ComboBox x:Name="cbNature" Grid.Row="3" Grid.Column="2" Visibility="{Binding SelectedIndex, Converter={StaticResource selectedIndexToVisibilityConverter}, ElementName=cbUnder}"
              ItemsSource="{Binding DataContext.NaturesOfGroup, RelativeSource={RelativeSource AncestorType={x:Type Page}}}" DisplayMemberPath="Nature"
              SelectedValue="{Binding NatureOfGroupID}" SelectedValuePath="NatureOfGroupID"/>

    <StackPanel Grid.Row="5" Grid.Column="4" Orientation="Horizontal">
        <Button Content="Save" Command="{Binding DataContext.SaveChangesCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}"/>
    </StackPanel>
</Grid>

我可以在您的代码中看到两个问题

首先你提到的

实际上我想显示GroupName及其相应的效果,但是 当用户选择任何项目时,我希望获得SelectedValue作为 父ID

但在组合框声明中,您可以设置
SelectedValuePath=“GroupID”
。应将其设置为ParentID-
SelectedValuePath=“ParentID”


Second,即使您将
SelectedValuePath
设置为
ParentID
,它也不会工作,因为combobox ItemsSource是一个具有相应效果的
组名列表,其中没有任何属性
ParentID

对于SelectedValuePath,底层模型类应该具有该属性,所以创建一个属性并像其他两个属性一样从数据库填充它

GroupNamesWithCorrespondingEffects = (from g in db.Groups
                             select new GroupNameWithCorrespondingEffect
                             {
                                GroupName = g.GroupName,
                                CorrespondingEffect = g.Master_Effects.Effect,
                                ParentID = g.ParentId
                             }).ToList();

因此,要使您的解决方案发挥作用,您必须解决这两个问题。

CurrentGroup
不受XAML的约束。您在哪里设置?CurrentGroup绑定到作为Combobox父级的网格。对不起,我忘了提这个问题。绑定到网格意味着什么?这是如何更新的?网格没有任何SelectedValue属性。这是如何与combobox选择的值同步的?请参阅我问题中的更新,以更好地了解Grid和CurrentGroup之间的绑定。请参阅问题的最底部。更新后的xaml中,我提到了
,在C中,就在您提到的行下方,我声明了
公共组CurrentGroup
。这里CurrentGroup的数据类型是Group,所以我认为它应该自动设置实际属性。如果我犯了一些错误,请给我一个演示示例来理解。我对您的第一个解决方案有一些问题:如果我设置SelectedValuePath=“ParentID”,那么我应该为SelectedValue设置什么值?这与您对第二个组合框所做的相同
SelectedValue=“{Binding NatureOfGroupID}”SelectedValuePath=“NatureOfGroupID”
。现在要澄清的是,
SelectedValue
指向实际的属性实例,您希望在组合框中跟踪所选对象,
SelectedValuePath
表示如果对象绑定到对象,那么哪个属性应该作为SelectedValue的路径。因此,在您的情况下,两者都将是
ParentID
,就像您为第二个combobox所做的那样。我尝试了您的第二个解决方案,也阅读了您的上述注释,并按照您的建议进行了更改,但现在我总是
CurrentGroup.ParentID=1
将断点放在
GroupNamesWithCorrespondingEffects = (from g in db.Groups
                             select new GroupNameWithCorrespondingEffect
                             {
                                GroupName = g.GroupName,
                                CorrespondingEffect = g.Master_Effects.Effect,
                                ParentID = g.ParentId
                             }).ToList();