【问题标题】:.NET 4.x WPF ComboBox Databinding SelectedIndex not working.NET 4.x WPF 组合框数据绑定 SelectedIndex 不起作用
【发布时间】:2014-12-23 14:21:42
【问题描述】:

我正在将我的项目从 .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。但在列表中“geselecteerdProduct”(与 SelectedIndex 的数据绑定)设置为 0。

在 .NET 3.5 中它运行良好,但在 4.x SelectedIndex 中,“afm”中的值自动设置为 -1。

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

有人可以帮我吗?

【问题讨论】:

  • 所选索引不应该在 geselecteerdProduct 中吗?你在哪里/如何使用它?您只是出于某种原因将其设置为 0 ...
  • 没错。 geselecteerdProduct 是我的 selectedIndex。我将其设置为 0 以选择第一项。
  • “结果”窗口中是否有任何绑定错误/警告?如果在 SelectionChanged 事件上触发,您的 UpdateList 方法中发生了什么?
  • 当您将 SelectedIndex 绑定到属性时,会触发 SelectionChanged 事件。您是否有可能在“UpdateList”中做了一些可能导致这种行为的事情?
  • 更新列表为空。

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


【解决方案1】:

我认为这是 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    • 2011-08-05
    • 2012-08-07
    • 2014-03-30
    • 1970-01-01
    相关资源
    最近更新 更多