WPF ListBox改写模板,双击选中和删除问题

ListBox控件中,修改样式ItemTemplate,中外层为Grid,左侧为TextBlock显示内容,右侧为Button,button主要响应删除功能,双击文本,选中内容添加到指定区域;

首先来看看前端XAML设计

<ListBox  x:Name="Dic_ListBox"  Width="250" Height="700" HorizontalAlignment="Center" VerticalAlignment="Top"  SelectionMode="Single" MouseDoubleClick="Listbox_MouseDoubleClick" >
	<ListBox.ItemTemplate>
		<DataTemplate>
			<Grid Width="220">
				<Grid.ColumnDefinitions>
					<ColumnDefinition/>
					<ColumnDefinition Width="auto"/>
				</Grid.ColumnDefinitions>
				<TextBlock FontSize="14" Text="列表内容" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Width="200" "/>
				<Button Visibility="Hidden" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5 0 5 0" Click="Del_Click" CommandParameter="{Binding }"/>
			</Grid>
		</DataTemplate>
	</ListBox.ItemTemplate>
</ListBox>

后台实现代码

private void Listbox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
	if (Dic_ListBox.SelectedItem != null)
	{
		Dictionary model = Dic_ListBox.SelectedItem as Dictionary;
		if (model != null)
		{
		//处理事件
		}
	}
}

public class Dictionary
{
       public int Dict_id { get; set; }
       public string Dict_name { get; set; }
}

private void Del_Click(object sender, RoutedEventArgs e)
{
	Button btn = sender as Button;
	if (btn != null)
	{
		Dictionary model = btn.CommandParameter as Dictionary;
		if (model != null)
		{
			m_DictionaryList.Remove(model);
			Dic_ListBox.ItemsSource = null;
			Dic_ListBox.ItemsSource = m_DictionaryList;
		}
	}
}

但是在测试过程中发现问题:

异常问题1:使用ListBox中的MouseDoubleClick,双击Button也触发选中,并且删除;
异常问题2:快速双击Button响应Click事件或者MouseDown事件,删除两个Item子项,而不是一个item项;
解决问题1:去除ListBox中的MouseDoubleClick事件,在Grid中添加MouseLeftButtonDown事件,响应双击文本功能;注意文本显示框要设置高度,不然点击空白处,不想要MouseDoubleClick事件
解决问题2:采用Button的Click事件,根据鼠标模拟双击事件的时间,间隔300ms内为双击,300ms内多次点击Button只响应一次;

解决代码参考如下:

<ListBox  x:Name="Dic_ListBox" Width="250" Height="700" HorizontalAlignment="Center" VerticalAlignment="Top" SelectionMode="Single" >
	<ListBox.ItemTemplate>
		<DataTemplate>
			<Grid Width="220" MouseLeftButtonDown="ImageRemarker_MouseLeftButtonDown" HorizontalAlignment="Center" VerticalAlignment="Center">
				<Grid.ColumnDefinitions>
					<ColumnDefinition/>
					<ColumnDefinition Width="auto"/>
				</Grid.ColumnDefinitions>
				<TextBlock FontSize="14" Text="列表内容" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Width="200" "/>
				<Button Visibility="Hidden" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5 0 5 0" Click="Del_Click" CommandParameter="{Binding }"/>
			</Grid>
		</DataTemplate>
	</ListBox.ItemTemplate>
</ListBox>
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
	if (e.LeftButton == MouseButtonState.Pressed && e.ClickCount == 1)
	{
		if (e.OriginalSource != null && e.OriginalSource is TextBlock)
		{
			var control = e.OriginalSource as FrameworkElement;
			if (control == null || control.DataContext == null)
				return;
		}
	}
	else if (e.LeftButton == MouseButtonState.Pressed && e.ClickCount >= 2)
	{
		if (e.OriginalSource != null && e.OriginalSource is Button)
		{
			e.Handled = true;
		}
		else if (e.OriginalSource != null && e.OriginalSource is TextBlock)
		{
			var control = e.OriginalSource as FrameworkElement;
			if (control == null || control.DataContext == null)
				return;
			 var dic = Dic_ListBox.SelectedItem as Dictionary;
		}
	}
}

List<Dictionary> m_DictionaryList = null;//存储列表数据
DateTime m_mouseClikTime = DateTime.Now;
private void Del_Click(object sender, RoutedEventArgs e)
{
	if (DateTime.Now.Subtract(m_mouseClikTime).TotalMilliseconds > 300)
	{
		m_mouseClikTime = DateTime.Now;
		Button btn = sender as Button;
		if (btn != null)
		{
			Dictionary model = btn.CommandParameter as Dictionary;
			if (model != null)
			{
				m_DictionaryList.Remove(model);
				Dic_ListBox.ItemsSource = null;
				Dic_ListBox.ItemsSource = m_DictionaryList;
			}
		}
	}
}


public class Dictionary
{
       public int Dict_id { get; set; }
       public string Dict_name { get; set; }
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值