質問

私は持っています StackPanel 複数の Expanders:

<StackPanel Margin="0,10,0,0">
    <Expander Header="Test 1">
        <ListBox>
            <ListBoxItem Content="Unit 1"/>
            <ListBoxItem Content="Unit 2"/>
        </ListBox>
    </Expander>
    <Expander Header="Test 2">
        <ListBox>
            <ListBoxItem Content="Unit 3"/>
            <ListBoxItem Content="Unit 4"/>
        </ListBox>
    </Expander>
</StackPanel>

そして、これらの動作を実装したいと思います。

  • 一方または両方 Expander(s) は拡張できる
  • 唯一 Expander アクティブになる可能性があります (ヘッダーの背景を変更)
  • アクティブな Expander 別のものを選択すると変わります Expander ただし、パネル内で他のものを選択すると、 Expander またはパネルの外側の他のコントロールがアクティブです Expander 滞在します

どうすればこれを達成できますか?

役に立ちましたか?

解決

の代わりのStackPanelのListControlにそれらを追加します。 ListControlsは、項目を選択サポートします。

XAMLます:

<Window x:Class="ExpanderTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">

    <Window.Resources>

        <Style TargetType="ListBoxItem">
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Red"/>
            </Style.Resources>

        </Style>

    </Window.Resources>

    <StackPanel Margin="0,10,0,0">
        <ListBox SelectedIndex="1">
            <ListBoxItem HorizontalContentAlignment="Stretch">
                <Expander Header="Test 1">
                    <ListBox>
                        <ListBoxItem Content="Unit 1"/>
                        <ListBoxItem Content="Unit 2"/>
                    </ListBox>
                </Expander>
            </ListBoxItem>
            <ListBoxItem HorizontalContentAlignment="Stretch">
                <Expander Header="Test 2" >
                    <ListBox>
                        <ListBoxItem Content="Unit 3"/>
                        <ListBoxItem Content="Unit 4"/>
                    </ListBox>
                </Expander>
            </ListBoxItem>
        </ListBox>
    </StackPanel>

</Window>

の後ろのコード:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ExpanderTest
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            EventManager.RegisterClassHandler(typeof(UIElement),
                                         GotFocusEvent,
                                         new RoutedEventHandler(OnGotFocus));
        }

        private static void OnGotFocus(object sender, RoutedEventArgs e)
        {
            // Check if element that got focus is contained by a listboxitem and
            // in that case selected the listboxitem.

            DependencyObject parent = e.OriginalSource as DependencyObject;
            while (parent != null)
            {
                ListBoxItem clickedOnItem = parent as ListBoxItem;
                if (clickedOnItem != null)
                {
                    clickedOnItem.IsSelected = true;
                    return;
                }

                parent = VisualTreeHelper.GetParent(parent);
            }
        }
    }
}

のコードが背後に何をするか、この記事に私の答えを参照してください:<のhref =「https://stackoverflow.com/questions/1459786/selecting-a-listboxitem-when-its-inner-combobox-is-focused」タイトル> =「その内側のコンボボックスがフォーカスされるときにListBoxItemの選択」リンクテキストの

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top