.NET 4.x WPF ComboBox数据绑定SelectedIndex无法正常工作

时间:2014-10-28 08:41:08

标签: c# .net wpf data-binding combobox

我将我的项目从.NET Framework 3.5更新到.NET Framework 4.5.1。一切正常,除了我的数据绑定到ComboBox。 ComboBox是Itemscontrol的一部分。 XAML代码:

<ItemsControl x:Name="AfmetingenLijst" BorderBrush="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}" BorderThickness="0" VerticalContentAlignment="Bottom"  >
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Grid Margin="5,5,5,5">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="2*" />
                                    <ColumnDefinition Width="2*" />
                                    <ColumnDefinition Width="2*" />
                                    <ColumnDefinition Width="2*" />
                                    <ColumnDefinition Width="2*" />
                                    <ColumnDefinition Width="1*" />
                                </Grid.ColumnDefinitions>
                                <TextBox Grid.Column="0" Text="{Binding Lengte}" Tag="{Binding LID}" Margin="5,5,5,5"  TextChanged="UpdateList"/>
                                <TextBox Grid.Column="1" Text="{Binding Breedte}" Tag="{Binding BID}" Margin="5,5,5,5" TextChanged="UpdateList"/>
                                <TextBox Grid.Column="2" Text="{Binding Hoogte}" Tag="{Binding HID}" Margin="5,5,5,5" TextChanged="UpdateList"/>
                                <TextBox Grid.Column="3" Text="{Binding Naam}" Margin="5,5,5,5" />
                                <ComboBox Grid.Column="4" ItemsSource="{Binding Path=items}" SelectedIndex="{Binding geselecteerdProduct}" SelectedValuePath="Code" DisplayMemberPath = "Naam" SelectionChanged="UpdateList" Height="25" HorizontalAlignment="Stretch" VerticalAlignment="Center"></ComboBox>
                                <Button Tag="{Binding ID}" Grid.Column="5" Margin="5,5,5,5" Content="{Binding Title}" Click="Afmeting_handler" />
                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

在C#中,列表中填充了数据绑定值:

afm = new List<Afmeting>();
        if (afm.Count == 0)
        {
            afm.Add(new Afmeting() { ID = "+", LID = "L+", BID = "B+", HID = "H+", Title = "+", items = items });
            afm[afm.Count - 1].geselecteerdProduct = 0;
            afmetingen_counter++;


        }
        AfmetingenLijst.ItemsSource = afm;

ComboBox中的项目存在,我可以通过鼠标选择它们。但默认情况下SelectedIndex = -1。但在列表&#34; geselecteerdProduct&#34; (对SelectedIndex的数据绑定)设置为0.

在.NET 3.5中,它运行得很完美,但是在4.x SelectedIndex和&#34; afm&#34;中的值。自动设置为-1。

项目不为空,列表中有+ - 5项。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我认为这是WPF的一个错误。

当ComboBox嵌套在ItemsControl中,并同时设置SelectedIndex和SelectedValuePath时,ComboBox的OnSelectionChanged将在初始化时触发,但SelectionChanged只触发一次。 WPF ComboBox SelectedIndex debug

要解决问题很简单,只需从ComboBox中删除SelectedValuePath。

XAML:

<ItemsControl x:Name="AfmetingenLijst" BorderBrush="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}" BorderThickness="0" VerticalContentAlignment="Bottom"  >
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid Margin="5,5,5,5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="2*" />
                    <ColumnDefinition Width="2*" />
                    <ColumnDefinition Width="2*" />
                    <ColumnDefinition Width="2*" />
                    <ColumnDefinition Width="2*" />
                    <ColumnDefinition Width="1*" />
                </Grid.ColumnDefinitions>
                <TextBox Grid.Column="0" Text="{Binding Lengte}" Tag="{Binding LID}" Margin="5,5,5,5"  TextChanged="UpdateList"/>
                <TextBox Grid.Column="1" Text="{Binding Breedte}" Tag="{Binding BID}" Margin="5,5,5,5" TextChanged="UpdateList"/>
                <TextBox Grid.Column="2" Text="{Binding Hoogte}" Tag="{Binding HID}" Margin="5,5,5,5" TextChanged="UpdateList"/>
                <TextBox Grid.Column="3" Text="{Binding Naam}" Margin="5,5,5,5" />
                <ComboBox Grid.Column="4"
                        ItemsSource="{Binding Path=items}"
                        SelectedIndex="{Binding geselecteerdProduct}"

                        DisplayMemberPath = "Naam"
                        SelectionChanged="UpdateList"
                        Height="25"
                        HorizontalAlignment="Stretch"
                        VerticalAlignment="Center">
                </ComboBox>
                <Button Tag="{Binding ID}" Grid.Column="5" Margin="5,5,5,5" Content="{Binding Title}" Click="Afmeting_handler" />
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>