Fragensteller
WPF Combobox Vorauswahl funktioniert nicht

Allgemeine Diskussion
-
Hallo Community,
bei einer Combobox möchte ich nach dem Hinzufügen eines neuen Elements dieses als Vorauswahl markieren.
Leider funktioniert es nicht wie gewünscht.
Hier mein Code:
AddValue add = new AddValue(); add.ShowDialog(); DataReader.manufacturercollection.Add(MainWindow.tempstring); cbx_manufacturer.SelectedValue = MainWindow.tempstring;
Kurz zur Vorgehensweise: Der neue Wert wird im AddValue-Fenster eingelesen und in MainWindow.tempstring gespeichert.
Danach wird dieser zur manufacturercollection (ist eine ObservableCollection) hinzugefügt. Dieser Wert soll dann vorausgewählt sein. Das Hinzufügen zur Collection klappt auch problemlos und in der Combobox wird der Wert auch aufgeführt an letzter Stelle. Aber auch eine Auswahl über den SelectedIndex ist nicht möglich.
Wie kann ich das Problem lösen?
Vielen Dank
Update-Troubleshooter
- Typ geändert Ivan DragovMicrosoft contingent staff, Moderator Freitag, 7. Juli 2017 05:17 Keine bestätigte Lösung
Alle Antworten
-
Hallo,
hast du ein SelectedValuePath auf der ComboBox gesetzt? Wenn nicht und die ObservableCollection direkt die anzuzeigenden Strings enthält, dann versuche mal SelectedItem zuzuweisen.
Das SelectedIndex nicht zuzuweisen geht finde ich dann allerdings schon seltsam. Hast du bedacht dass der größte Index Count-1 ist?
Wenn du die Listeneinträge bindest, warum dann nicht das ausgewählte Element?
Viele Grüße, Tom Lambert - MVP, MCC und MSP
Wozu Antworten markieren und Posts bewerten? Klicke hier
Nützliche Links: .NET Quellcode | C#/VB.NET Konverter | GitHub Forum Samples | Account bestätigen (Verify Your Account)
Ich: Webseite | Facebook | Twitter | Code Snippets | GitHub -
Hallo,
Leider hat das
cbx_manufacturer.SelectedItem = MainWindow.tempstring;
keinen Erfolg gebracht und auch
cbx_manufacturer.SelectedIndex = DataReader.manufacturercollection.Count-1;
funktioniert nicht. Als Vorauswahl steht weiterhin "Neu" in der Combobox. Ein Binding habe ich nicht. Die ObservableCollection enthält direkt die Strings.
Gruß
Update-Troubleshooter
-
Hallo Troubleshooter,
vielleicht hilft Dir der nachfolgende Code weiter:
MainWindow.xaml<Window x:Class="Test.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Test" mc:Ignorable="d" Loaded="Window_Loaded" Title="MainWindow" Height="350" Width="525"> <Window.DataContext> <local:MainWindowViewModel/> </Window.DataContext> <Window.Resources> <CollectionViewSource x:Key="stringsViewSource" Source="{Binding Path=Strings}"/> </Window.Resources> <DockPanel LastChildFill="False" > <StackPanel Orientation="Horizontal" DockPanel.Dock="Top"> <ComboBox Name="testComboBox" Width="150" ItemsSource="{Binding Mode=OneWay, Source={StaticResource stringsViewSource}}" /> <TextBox Width="150" Text="{Binding NewEntry}"/> <Button Content="Hinzufügen" Name="addButton" Click="addButton_Click"/> </StackPanel> </DockPanel> </Window>
MainWindow.xaml.csusing System.Windows; namespace Test { public partial class MainWindow : Window { MainWindowViewModel _context; public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { _context = DataContext as MainWindowViewModel; } private void addButton_Click(object sender, RoutedEventArgs e) { if (!_context.Strings.Contains(_context.NewEntry)) { _context.Strings.Add(_context.NewEntry); } else { MessageBox.Show(string.Format("'{0}' ist schon in der Liste", _context.NewEntry)); } testComboBox.SelectedItem = _context.NewEntry; } } }
MainWindowViewModel.csusing System.Collections.ObjectModel; using System.ComponentModel; using System.Runtime.CompilerServices; namespace Test { public class MainWindowViewModel :INotifyPropertyChanged { public MainWindowViewModel() { Strings = new ObservableCollection<string>(); } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } private string _newEntry; public string NewEntry { get { return _newEntry; } set { if (_newEntry != value) { _newEntry = value; NotifyPropertyChanged(); } } } public ObservableCollection<string> Strings { get; private set; } } }