我在ListView中有一个可以绑定到itemsource的组合框。在组合框中选择一个值时,我的选定项目未设置。有人可以告诉我为什么我的选定项目(NumberOfIterations)没有设置?当我将它移到ListView之外时,组合框工作,让我觉得我没有完全正确的绑定,但我无法弄清楚出了什么问题。我使用SimpleMVVMToolkit框架。
<ListView Grid.Row="1" Grid.Column="2"
ItemsSource="{Binding SequenceListItems}"
SelectedItem="{Binding SequenceListItemSelected}"
ItemContainerStyle="{StaticResource alternatingListViewItemStyle}"
AlternationCount="2">
<ListView.View>
<GridView>
<GridViewColumn Header="# of Iterations" Width="125">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<ComboBox ItemsSource="{Binding
SequenceEditorViewModel.Iterations, Source={StaticResource ViewModelLocator}}"
SelectedItem="{Binding Path=NumberOfIterations, Mode=TwoWay}" SelectedIndex="0"
FontWeight="Bold" VerticalAlignment="Center" Width="60" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
ViewModel Items ...我目前没有使用模型
// Constructor
public SequenceEditorViewModel()
{
// Populate the Iterations for displaying in the combobox in the gridview.
for (int x = 1; x <= 50; x++)
{
this.Iterations.Add(x);
}
}
//public property (Iterations is source for combobox and NumberOfIterations
//is for the selected item in the combobox)
public ObservableCollection<int> Iterations
{
get
{
return this.iterations;
}
set
{
this.iterations = value;
this.NotifyPropertyChanged(m => m.Iterations);
}
}
public int NumberOfIterations
{
get
{
return this.numberOfIterations;
}
set
{
this.numberOfIterations = value;
this.NotifyPropertyChanged(m => m.NumberOfIterations);
}
}