1

WPF ComboBoxの操作中に問題に直面しています。私の状況では、いくつかの値を表示している ComboBox があります。ContentControlComboBox のItemsプロパティに s を追加しています。Contentこれらの ContentControl を何らかのデータソースにバインドして、コンテンツを動的に変更できるようにしました。問題は、選択されたアイテムのコンテンツが ComboBox ドロップダウンの更新のアイテムを変更するが、ComboBox SelectionChange のアイテムが同じままである場合です。

何か提案はありますか?

4

3 に答える 3

1

ComboBox 内に ContentControl を直接追加する代わりに、DataTemplate(ItemsTemplate) または ItemContainerStyle を使用します。ContentControl が Mousedown を消費して ComboboxItem を非表示にするため、自動生成された ComboBoxItem はクリックを認識しないためです。ComboBox アイテムは IsSelectedProperty を設定し、SelectionChanged をトリガーします。

于 2009-10-22T17:14:01.287 に答える
1

この問題は別の方法で解決しました。
ComboBox SelectedItem は、表示されるものではなく、選択するものです。Item を選択すると、comboBox は SelectedItem の Template ではなく、SelectionBox の Template に表示されます。ComboBox の VisualTree に移動すると、TextBlock を含む ContentPresenter があり、この TextBlock に選択した項目のテキストが割り当てられていることがわかります。
だから、SelectionChanged イベント ハンドラーで、VisualTreeHelper を使用して ContentPresenter 内の TextBlock を探し、この TextBlock の Text プロパティを ContentControl(SelectedItem) の Content プロパティにバインドしました。

編集:

ComboBox の SelectionChanged evetn ハンドラで、次のように書きました。

ModifyCombox(cmbAnimationBlocks, myComboBox.SelectedItem.As<ContentControl>());

このメソッドは次のとおりです。

    private static void ModifyCombox(DependencyObject comboBox, ContentControl obj)
    {
        if (VisualTreeHelper.GetChildrenCount(comboBox) > 0)
        {
            WalkThroughElement(comboBox, obj);
        }
    }

    private static void WalkThroughElement(DependencyObject element, ContentControl contentControl)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
        {
            if (element.GetType() == typeof(ContentPresenter))
            {
                ContentPresenter contentPresenter = element.As<ContentPresenter>();
                TextBlock textBlock = VisualTreeHelper.GetChild(contentPresenter, 0).As<TextBlock>();
                textBlock.SetBinding(TextBlock.TextProperty, new Binding("Content")
                {
                    Source = contentControl
                });
                contentPresenter.Content = textBlock;
            }
            else
            {
                DependencyObject child = VisualTreeHelper.GetChild(element, i).As<FrameworkElement>();
                WalkThroughElement(child, contentControl);
            }
        }

        if (VisualTreeHelper.GetChildrenCount(element) == 0 && element.GetType() == typeof(ContentPresenter))
        {
            ContentPresenter contentPresenter = element.As<ContentPresenter>();
            TextBlock textBlock = new TextBlock();
            textBlock.SetBinding(TextBlock.TextProperty, new Binding("Content")
            {
                Source = contentControl
            });
            contentPresenter.Content = textBlock;
        }
    }
于 2009-10-23T13:38:12.170 に答える