我正在将我的项目从.NET Framework 3.5更新到.NET Framework 4.5.1。一切正常,除了我的数据绑定到一个ComboBox。组合框是Itemscontrol的一部分。该XAML代码:.NET 4.x WPF组合框数据绑定SelectedIndex不起作用
<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 。
项目不为空,列表中有+ - 5项。
有人可以帮助我吗?
未在选定的索引应该在'geselecteerdProduct'?你在哪里/如何使用它?你只是由于某种原因将其设置为0 ... – Noctis 2014-10-28 09:00:20
那就对了。 geselecteerdProduct是我的selectedIndex。我将它设置为0来选择第一个项目。 – 2014-10-28 09:36:18
您的结果窗口中是否存在任何绑定错误/警告?如果在'SelectionChanged'事件上触发''UpdateList''方法会发生什么? – Jerrington 2014-10-28 09:37:07