6

選択した値が変更されるたびに個別のコマンドをトリガーする単純な ComboBox があります。これが私のマークアップの例です:

<WrapPanel Grid.Row="0" Visibility="{Binding ShowToggleViewFeedViewManual}">
    <ComboBox Margin="3,3,0,0">
        <ComboBoxItem IsEnabled="{Binding CanSelectViewFeedData}" >
            <ComboBoxItem.CommandBindings>
                <CommandBinding Command="SelectViewFeedDataCommand" />
            </ComboBoxItem.CommandBindings>
            <TextBlock Text="View Feed Data"/>
        </ComboBoxItem>
        <ComboBoxItem IsEnabled="{Binding CanSelectViewManualData}">
            <ComboBoxItem.CommandBindings>
                <CommandBinding Command="SelectManualFeedDataCommand" />
            </ComboBoxItem.CommandBindings>
            <TextBlock Text="View Manual Data"/>
        </ComboBoxItem>
    </ComboBox>
</WrapPanel>   

「'SelectViewFeedDataCommand' を変換できません」というエラーが表示されます。他の ComboBoxItem でも同様のエラーが発生します。ICommand は、DataTemplate としてバインドされた UserControl の DataSource である ViewModel クラスで定義されます。

public ICommand SelectViewFeedDataCommand
{
    get
    {
        // Code to perform
    }
}

私はこれをかなり広範囲に調査しましたが、ICommand を ComboBoxItem に効果的にバインドする方法についての答えは見つかりませんでした。

一連のラジオボタンと関連するコマンドを使用する既存のコードからこれを適応させていますが、これは非常に簡単に実行できます。ComboBox でこれを行う簡単な方法はありませんか?

ありがとう。

4

2 に答える 2

0

コマンドをバインディングに入れてみましたか?

<CommandBinding Command="{Binding SelectManualFeedDataCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />

更新された戦略の編集:

ComboBox アイテムは直接のコマンド バインドをサポートしていないため、添付プロパティを作成してみてください。

リンク

于 2012-07-23T15:15:31.150 に答える
0

私は自分の古い投稿を見ていて、自分の質問に対する解決策を投稿したことがないことに気付きました...本当に非常に単純です(エレガントではないにしても)。コンボ ボックスの「選択肢」を含む ViewModel にリストを作成したところ、SelectedValue が変更されたときに、プロパティのセッターで「変更された」ロジックがトリガーされました。

<WrapPanel Grid.Row="0" Visibility="{Binding ShowToggleFeedViewManual}" >
    <ComboBox Margin="10,10,10,10" Width="200" ItemsSource="{Binding AvailableDataSources}" SelectedValue="{Binding SelectedDataSource}"/>
</WrapPanel>

そしてビューモデルから:

public FeedSource SelectedDataSource
    {
        get { return _selectedDataSource; }
        set
        {
            _selectedDataSource = value;
            base.OnPropertyChanged("SelectedDataSource");

            //additional code to perform here

        }
    }
于 2015-07-16T16:44:07.590 に答える