0

我有以下 xaml:

<UserControl.Resources>
    <sivm:Locator x:Key="viewModelLocator" ModelType="{x:Type ViewModels:RateVSConcentrationViewModel}"/>
</UserControl.Resources>

<UserControl.DataContext>
    <Binding Path="ViewModel" Source="{StaticResource viewModelLocator}"/>
</UserControl.DataContext>

...

<ComboBox Grid.Column="0" Grid.Row="1" Height="20" VerticalAlignment="Top" ItemsSource="{Binding Chambers}" > <!--SelectedItem="{Binding SelectedChamber}">-->
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding ChamberName}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Chambers是一个ObservableCollection包含ChamberName我希望在组合框中显示的属性的对象。

在 ViewModel 我有以下代码:

foreach (var chamber in chambers)
{
     Chambers.Add(chamber);
}
OnPropertyChanged(() => Chambers);

但是当此代码执行时,组合框不会更新。我已经使用这种方式进行了很多数据绑定,但我无法让它工作。

有人能看到我在这里做错了吗?

4

3 回答 3

0

You may need to set the relativesource on your textblock, try modifying the binding on the Text property of your textblock to the following:

{Binding DataContext.ChamberName, RelativeSource={RelativeSource AncestorType=ComboBox}}

And as nit said above, always check for binding errors, they will put you on the right path.

于 2013-10-28T14:11:54.453 回答
0

You should adjust the property binding as follows:

<ComboBox ItemsSource="{Binding Path=ViewModel.Chambers, Source={StaticResource viewModelLocator}}" >
于 2013-10-28T14:12:17.280 回答
0

我让它工作:

<ComboBox DisplayMemberPath="ChamberName" Grid.Column="0" Grid.Row="1" Height="20" VerticalAlignment="Top" ItemsSource="{Binding Chambers}"> <!--SelectedItem="{Binding SelectedChamber}">-->
于 2013-10-28T14:59:43.933 回答