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

私はこれを行うようにしました:

<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 に答える
0

テキストブロックに relativesource を設定する必要がある場合があります。テキストブロックの Text プロパティのバインディングを次のように変更してみてください。

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

そして、ニットが上で言ったように、常にバインディングエラーをチェックしてください。それらはあなたを正しい道に導きます。

于 2013-10-28T14:11:54.453 に答える