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# 在带有标题的组合框中使用多列时,SelectedIndex始终保持-1_C#_Wpf_Combobox_Selectedindex - Fatal编程技术网

C# 在带有标题的组合框中使用多列时,SelectedIndex始终保持-1

C# 在带有标题的组合框中使用多列时,SelectedIndex始终保持-1,c#,wpf,combobox,selectedindex,C#,Wpf,Combobox,Selectedindex,我有一个带有标题的多列组合框。通过使用答案,我已经做到了这一点 以下是我使用的XAML: <CollectionViewSource x:Key="GroupNamesWithCorrespondingEffectsCollection" Source="{Binding GroupNamesWithCorrespondingEffects}" /> <CompositeCollection x:Key="Items"> <ComboBoxItem IsE

我有一个带有标题的多列组合框。通过使用答案,我已经做到了这一点

以下是我使用的XAML:

<CollectionViewSource x:Key="GroupNamesWithCorrespondingEffectsCollection" Source="{Binding GroupNamesWithCorrespondingEffects}" />

<CompositeCollection x:Key="Items">
    <ComboBoxItem IsEnabled="False" Background="#FF2A2A2A" Foreground="White">
        <Grid TextElement.FontWeight="Bold" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition SharedSizeGroup="A" />
                <ColumnDefinition Width="50" />
                <ColumnDefinition SharedSizeGroup="B" />
            </Grid.ColumnDefinitions>
            <Grid.Children>
                <TextBlock Grid.Column="0" Text="Group Name" />
                <TextBlock Grid.Column="2" Text="Effect" />
            </Grid.Children>
        </Grid>
    </ComboBoxItem>
    <CollectionContainer Collection="{Binding Source={StaticResource GroupNamesWithCorrespondingEffectsCollection}}" />
</CompositeCollection>

<DataTemplate DataType="{x:Type helpers:GroupNameWithCorrespondingEffect}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition SharedSizeGroup="A" />
            <ColumnDefinition Width="50" />
            <ColumnDefinition SharedSizeGroup="B" />
        </Grid.ColumnDefinitions>
        <Grid.Children>
            <TextBlock Grid.Column="0" Text="{Binding GroupName}" />
            <TextBlock Grid.Column="2" Text="{Binding CorrespondingEffect}" />
        </Grid.Children>
    </Grid>
</DataTemplate>

<ComboBox ItemsSource="{DynamicResource Items}" 
          SelectedValue="{Binding GroupNameWithCorrespondingEffect}"
          SelectedValuePath="GroupID"
          DisplayMemberPath="GroupName" />

注意:

C#代码没有发布,因为我认为没有必要在这里发布。如果有人想看一下C代码,请告诉我&我会贴出来的

问题:


我想检查CodeBehind文件中ComboBox的SelectedIndex。但是我注意到SelectedIndex始终保持-1。有什么问题吗?我该如何克服它

您已将
SelectedValue
绑定到
具有相应效果的组名
,我怀疑它属于具有相应效果的组名类型,同时将
SelectedValue路径
绑定到
组ID
,可以是int,也可以是uint

SelectedValue和SelectedValuePath应始终为同一类型。在您的情况下,您可以删除SelectedValuePath并直接与SelectedValue绑定

<ComboBox ItemsSource="{DynamicResource Items}" 
          SelectedValue="{Binding GroupNameWithCorrespondingEffect}"
          DisplayMemberPath="GroupName" />


My combobox位于datagridtemplatecolumn内。GroupID是一个整数,它是GroupName的一个属性,具有相应的效果。如果我将ItemsSource从{DynamicResource Items}更改为{Binding DataContext.GroupNames with CorrespondingEffects,RelativeSource={RelativeSource Mode=FindancestorAncestorType={x:Type Page}}然后我得到了正确的SelectedIndex。您可以发布您试图获取SelectedIndex的位置吗?或者可以是复制您的问题的一小段代码示例?当我按Enter键时,我试图在PreviewKeyDown事件中获取SelectedIndex。我现在没有带笔记本电脑,所以我现在不能发布代码。我创建了一个简单的示例项目,一切正常。现在我怀疑我的SelectedValue绑定可能不正确。我的ComboBox位于DataGridTemplateColumn的CellEditingTemplate中。假设ComboBox绑定到具有三个属性(ID、Name和Age)的Person对象,那么ComboBox的SelectedValue应该绑定到哪个属性??绑定驻留在DataGrid中的ComboBox时,我非常困惑。你能用一个小例子或者一个好教程的链接来解释它吗?这完全取决于你对
SelectedValuePath
的选择。正如我在回答中提到的,
SelectedValuePath
SelectedValue
应该始终是相同的类型。如果尚未设置SelectedValuePath,SelectedValue类型应与ItemsSource集合的类型相同。因此,如果您已将
SelectedValuePath
设置为
ID
,则SelectedValue应绑定到int类型的属性,如果未设置
SelectedValuePath
,则应为
Person
类型。