Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有一种方法可以响应双击WPF中的ListBoxItem?_Wpf_Listbox_Eventsetter - Fatal编程技术网

有没有一种方法可以响应双击WPF中的ListBoxItem?

有没有一种方法可以响应双击WPF中的ListBoxItem?,wpf,listbox,eventsetter,Wpf,Listbox,Eventsetter,我有一个简单的WPF应用程序,其中有一个项目列表框。为了简化这个示例,我将其设置为字符串列表,但实际上,它将是某种复杂类型的列表。当列表框中的某个项目本身被双击时,我想响应它。显然,列表框中没有针对项目本身的直接双击事件。是否有一种简单的方法可以响应双击列表框中的项目(而不是列表框本身) 这是我的xaml: <Window x:Class="WpfApplication12.MainWindow" xmlns="http://schemas.microsoft.com/wi

我有一个简单的WPF应用程序,其中有一个项目列表框。为了简化这个示例,我将其设置为字符串列表,但实际上,它将是某种复杂类型的列表。当列表框中的某个项目本身被双击时,我想响应它。显然,列表框中没有针对项目本身的直接双击事件。是否有一种简单的方法可以响应双击列表框中的项目(而不是列表框本身)

这是我的xaml:

<Window x:Class="WpfApplication12.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox>
            <ListBox.Items>
                <sys:String>Item1</sys:String>
                <sys:String>Item2</sys:String>
                <sys:String>Item3</sys:String>
                <sys:String>Item4</sys:String>
                <sys:String>Item5</sys:String>
            </ListBox.Items>
        </ListBox>
    </Grid>
</Window>

项目1
项目2
项目3
项目4
项目5

这可以通过创建ItemContainerStyle和添加EventSetter轻松实现

像这样修改XAML:

<Window x:Class="WpfApplication12.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox>

            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                    <EventSetter Event="MouseDoubleClick" Handler="ListBoxItem_DoubleClick" />
                </Style>
            </ListBox.ItemContainerStyle>

            <ListBox.Items>
                <sys:String>Item1</sys:String>
                <sys:String>Item2</sys:String>
                <sys:String>Item3</sys:String>
                <sys:String>Item4</sys:String>
                <sys:String>Item5</sys:String>
            </ListBox.Items>
        </ListBox>
    </Grid>
</Window>
namespace WpfApplication12
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {     
        public MainWindow()
        {
            InitializeComponent();
        }
        private void ListBoxItem_DoubleClick(object sender, MouseButtonEventArgs e)
        {
            var listBoxItem = sender as ListBoxItem;
            if (listBoxItem != null)
            {
                var content = listBoxItem.Content as string;
                MessageBox.Show(content);
            }
        }
    }
}

项目1
项目2
项目3
项目4
项目5
隐藏的代码:

<Window x:Class="WpfApplication12.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox>

            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                    <EventSetter Event="MouseDoubleClick" Handler="ListBoxItem_DoubleClick" />
                </Style>
            </ListBox.ItemContainerStyle>

            <ListBox.Items>
                <sys:String>Item1</sys:String>
                <sys:String>Item2</sys:String>
                <sys:String>Item3</sys:String>
                <sys:String>Item4</sys:String>
                <sys:String>Item5</sys:String>
            </ListBox.Items>
        </ListBox>
    </Grid>
</Window>
namespace WpfApplication12
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {     
        public MainWindow()
        {
            InitializeComponent();
        }
        private void ListBoxItem_DoubleClick(object sender, MouseButtonEventArgs e)
        {
            var listBoxItem = sender as ListBoxItem;
            if (listBoxItem != null)
            {
                var content = listBoxItem.Content as string;
                MessageBox.Show(content);
            }
        }
    }
}
命名空间WpfApplication12
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{     
公共主窗口()
{
初始化组件();
}
private void ListBoxItem_双击(对象发送器,鼠标按钮ventargs e)
{
var listBoxItem=发送方作为listBoxItem;
如果(listBoxItem!=null)
{
var content=listBoxItem.content作为字符串;
MessageBox.Show(内容);
}
}
}
}
以下是指向MSDN页面的链接,其中解释了一些有关EventSetter的信息: