在内部ComboBox聚焦时选择ListBoxItem

时间:2009-09-22 12:23:29

标签: wpf xaml listbox datatemplate triggers

我有一个DataTemplate,它将是一个模板化的ListBoxItem,这个DataTemplate有一个 ComboBox在它有焦点时我想要ListBoxItem这个模板 代表被选中,这看起来对我来说。但遗憾的是它不起作用=(

所以这里真正的问题是在DataTemplate中可以获取或设置值 ListBoxItem.IsSelected的{​​{1}}属性是什么?

DataTemplate.Trigger

3 个答案:

答案 0 :(得分:9)

我找到了解决问题的方法。

问题是当你对listboxitem有一个控件,并且点击了控件时(比如输入文本或更改组合框的值),ListBoxItem就不会被选中。

这应该做的工作:

public class FocusableListBox : ListBox
{
    protected override bool IsItemItsOwnContainerOverride(object item)
    {
        return (item is FocusableListBoxItem);
    }

    protected override System.Windows.DependencyObject GetContainerForItemOverride()
    {
        return new FocusableListBoxItem();
    }
}

- >使用此FocusableListBox代替WPF的默认ListBox。

并使用此ListBoxItem:

public class FocusableListBoxItem : ListBoxItem
{
    public FocusableListBoxItem()
    {
        GotFocus += new RoutedEventHandler(FocusableListBoxItem_GotFocus);
    }


    void FocusableListBoxItem_GotFocus(object sender, RoutedEventArgs e)
    {
        object obj = ParentListBox.ItemContainerGenerator.ItemFromContainer(this);
        ParentListBox.SelectedItem = obj;
    }

    private ListBox ParentListBox
    {
        get
        {
            return (ItemsControl.ItemsControlFromItemContainer(this) as ListBox);
        }
    }

}

Treeview也存在此问题,但此解决方案不适用于Treeview,因为SelectedItem的{​​{1}}为Treeview。 所以,如果你能帮我看看Treeview,请注意; - )

答案 1 :(得分:8)

我发现我更喜欢使用它:

<Style  TargetType="ListBoxItem">
    <Style.Triggers>
        <Trigger Property="IsKeyboardFocusWithin" Value="True">
             <Setter Property="IsSelected" Value="True"></Setter>
        </Trigger>
    </Style.Triggers>
</Style>

简单,适用于所有列表框项目,无论内部是什么。

答案 2 :(得分:0)

不知道为什么你的触发器不起作用。要捕获组合框的get focus事件(或列表框项目中的任何控件),您可以使用附加的路由事件。如果在应用程序的其他部分中需要此行为,则可以将代码也放在派生列表框中。

XAML:

<Window x:Class="RoutedEventDemo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Specialized="clr-namespace:System.Collections.Specialized;assembly=System"
    xmlns:System="clr-namespace:System;assembly=mscorlib"
    Height="300" Width="300">

    <Window.Resources>

        <DataTemplate x:Key="myDataTemplate">
            <Grid>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding}" Margin="5,0"/>
                    <ComboBox Width="50">
                        <ComboBoxItem>AAA</ComboBoxItem>
                        <ComboBoxItem>BBB</ComboBoxItem>
                    </ComboBox>
                </StackPanel>
            </Grid>
        </DataTemplate>

    </Window.Resources>

    <Grid>

        <ListBox ItemTemplate="{StaticResource myDataTemplate}">
            <ListBox.ItemsSource>
                <Specialized:StringCollection>
                    <System:String>Item 1</System:String>
                    <System:String>Item 2</System:String>
                    <System:String>Item 3</System:String> 
                </Specialized:StringCollection>
            </ListBox.ItemsSource>
        </ListBox>

    </Grid>
</Window>

代码背后隐藏所有焦点事件。

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

namespace RoutedEventDemo
{
    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);
            }
        }
    }
}