Ich fange an, die Wände hier zu klettern. Ich habe ein Kombinationsfeld mit einer Liste von Elementen gefüllt, und ich versuche herauszufinden, welche ausgewählt wurde. Ich habe auch versucht, die Combox-Box so einzustellen, dass automatisch der 0. Eintrag in der Liste ausgewählt wurde. Aber das wird auch nicht funktionieren. Ich erhalte einen verbindlichen Ausdruckspfadfehler sowohl für GroupIndex als auch für SelectedGroup. Ich kann jedoch nicht herausfinden, was das Problem ist.Kann das SelectedItem oder den SelectedIndex eines Kombinationsfelds nicht abrufen
Combo Box XAML:
<ComboBox
Name="GroupComboBox"
SelectedIndex="{Binding Path=GroupIndex}"
SelectedItem="{Binding Path=SelectedGroup}"
DisplayMemberPath="Name"
SelectedValuePath ="Name"
ItemsSource="{Binding Path=Groups}"
Height="38"
HorizontalAlignment="Left"
Margin="159,115,0,0"
VerticalAlignment="Top"
Width="185"
FontSize="24" Text="Select a Group" IsEditable="False" IsReadOnly="False" />
Hier ist der Code für das Füllen des Kombinationsfeld.
void webService_GroupListChanged(string response)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
IList<JSONGroup> listOfGroups = new List<JSONGroup>();
listOfGroups = serializer.Deserialize<List<JSONGroup>>(response);
Application.Current.Dispatcher.BeginInvoke((Action)(() =>
{
Groups = new ObservableCollection<Group>();
foreach(JSONGroup group in listOfGroups)
{
Groups.Add(new Group(group.name, group.id));
}
}));
}
private int groupIndex;
private int GroupIndex
{
get { return this.groupIndex; }
set
{
if (this.groupIndex != value)
{
this.groupIndex = value;
this.OnPropertyChanged("GroupIndex");
}
}
}
private Group selectedGroup;
private Group SelectedGroup
{
get { return this.selectedGroup; }
set
{
if (this.selectedGroup != value)
{
this.selectedGroup = value;
this.OnPropertyChanged("SelectedGroup");
}
}
}
private ObservableCollection<Group> groups;
public ObservableCollection<Group> Groups
{
get { return this.groups; }
set
{
if (this.groups != value)
{
this.groups = value;
this.OnPropertyChanged("Groups");
}
}
}
Setzen Sie den DataContext? Und wenn, was zu? – Phil
Der Datenkontext der Sicht (ein Benutzersteuerelement) wird auf das Ansichtsmodell gesetzt, in dem sich der obige Code befindet. Die Bindung für die tatsächliche Liste von Elementen in der Kombinationsbox funktioniert, also sollten theoretisch auch die anderen beiden Bindungen funktionieren. – benjgorman
Sollte dann funktionieren. Sie benötigen nur eines von SelectedItem und SelectedIndex. – Phil