WPF ListBox 鼠标左键双击

WPF  ListBox  鼠标左键双击

XAML

 

<!--鼠标左键双击-->
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding  Path=Text}" ToolTip="鼠标左键双击,采集本年度!">
                    <TextBlock.InputBindings>
                        <MouseBinding Command="{Binding DataContext.SelectListBoxItemCommand, ElementName=mainWin }" 
                                              CommandParameter="{Binding ElementName=lst}"  
                                              MouseAction="LeftDoubleClick"></MouseBinding>
                    </TextBlock.InputBindings>
                </TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>

 DataContext.SelectListBoxItemCommand 绑定的命令

ElementName=mainWin                                                          这里是当前窗口名称,也就是DataContext的宿主
CommandParameter="{Binding ElementName=lst}"              lst为ListBox名称
 MouseAction="LeftDoubleClick"                                             Action类型

 

 

 

Model 

public class TextItem
{
   public TextItem(string text)
   {
            Text = text;
   }

   public string  Text { get; set; }
        
}

 ViewModel


        private ICommand _SelectListBoxItemCommand;
        public ICommand SelectListBoxItemCommand
        {
            get
            {
                if (this._SelectListBoxItemCommand == null)
                    this._SelectListBoxItemCommand = new RelayCommandBase(SelectList_MouseDoubleClick);
                return this._SelectListBoxItemCommand;
            }
        }

        private void SelectList_MouseDoubleClick(object obj)
        {
           var lst= obj as ListBox;
            TextItem textItem = lst.SelectedItem as TextItem;

            //Task.Run(()=> GetOneYear(textItem));//实现
           
        }

 obj可以接收 CommandParameter 传递的参数。即Listbox

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要实现WPF Listbox鼠标悬浮效果,可以使用ListBox.ItemContainerStyle和Trigger来实现。具体步骤如下: 1. 在ListBox中添加ItemContainerStyle属性,指定ListBoxItem的样式: ``` <ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle}"> <ListBoxItem>Item 1</ListBoxItem> <ListBoxItem>Item 2</ListBoxItem> <ListBoxItem>Item 3</ListBoxItem> </ListBox> ``` 2. 在资源字典中定义ListBoxItemStyle样式: ``` <Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}"> <Setter Property="Background" Value="Transparent" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="Padding" Value="5" /> <Setter Property="HorizontalContentAlignment" Value="Stretch" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <Border x:Name="border" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"> <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="border" Property="Background" Value="#FFCCE5FF" /> </Trigger> <Trigger Property="IsSelected" Value="True"> <Setter TargetName="border" Property="Background" Value="#FF007ACC" /> <Setter Property="Foreground" Value="White" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> ``` 3. 在ListBoxItemStyle样式中添加IsMouseOver Trigger,指定鼠标悬浮时的背景色。 4. 可以根据需要,添加IsSelected Trigger,指定选中时的样式。 这样就可以实现WPF Listbox鼠标悬浮效果了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值