这是为什么呢?比如click事件,如果是UserContorl不响应,但是改成Window窗体后又是正常的,怎么解决这么情况呢,求教。
你的自定义控件继承了button吗? 你把你写的代码贴出来吧。
<UserControl x:Class="WpfApplication1.autoBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <UserControl.Resources> <DataTemplate x:Key="listboxtemplate"> <StackPanel> <Image Source="{Binding}" Visibility="{Binding ImgVisibility}" Width="16" Height="16" Stretch="Fill" Margin="2"></Image> <Button Content="{Binding ButtonContent}" Visibility="{Binding BtnVisibility}" Foreground="Blue" Click="Button_Click" /> <TextBlock Text="{Binding Path=Text}" Margin="2" ></TextBlock> </StackPanel> </DataTemplate> </UserControl.Resources> <StackPanel> <TextBox Name="tb" LostFocus="tb_LostFocus"/> <ListBox Name="lb" MaxHeight="150" Canvas.Left="0" Canvas.Top="20" Visibility="Visible" ItemTemplate="{StaticResource listboxtemplate}"> </ListBox> </StackPanel> </UserControl>
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication1 { /// <summary> /// Interaction logic for autoBox.xaml /// </summary> public partial class autoBox : UserControl { #region Members private ObservableCollection<AutoCompleteEntry> autoCompletionList;//数据源 public ObservableCollection<AutoCompleteEntry> AutoCompletionList { get { return autoCompletionList; } set { autoCompletionList = value; } } private ObservableCollection<AutoCompleteEntry> selectList;//筛选后的数据 public ObservableCollection<AutoCompleteEntry> SelectList { get { return selectList; } set { selectList = value; } } private delegate void TextChangedCallback(); private bool insertText;//是否插入textbox private int searchThreshold;//文本框最少输入多少字符开始匹配 public int Threshold { get { return searchThreshold; } set { searchThreshold = value; } } private bool autoBoxIsEnable = true;//是否可用 public bool AutoBoxIsEnable { get { return autoBoxIsEnable; } set { autoBoxIsEnable = value; } } public object SelectListItem //当前选择项 { get { return lb.SelectedItem; } set { lb.SelectedItem = value; } } private bool isAddButtonInTheFirst = false;//是否第一行添加按钮 public bool IsAddButtonInTheFirst { get { return isAddButtonInTheFirst; } set { isAddButtonInTheFirst = value; } } private double boxWidth; public double BoxWidth { get { return tb.Width; } set { tb.Width = value; lb.Width = value; } } private double boxHeight; public double BoxHeight { get { return tb.Height; } set { tb.Height = value; } } private double boxMaxHeight; public double BoxMaxHeight { get { return lb.MaxHeight; } set { lb.MaxHeight = value; } } private string imgSource; public string ImgSource { get { return imgSource; } set { imgSource = value; } } #endregion #region Constructor public autoBox() { InitializeComponent(); } public void Init(List<AutoCompleteEntry> list, bool isAddButton)//初始化 { autoCompletionList = new ObservableCollection<AutoCompleteEntry>(list); searchThreshold = 1; lb.IsEnabled = AutoBoxIsEnable; lb.IsSynchronizedWithCurrentItem = true; lb.IsTabStop = false; lb.Visibility = Visibility.Collapsed; tb.TextChanged += new TextChangedEventHandler(textBox_TextChanged); tb.VerticalContentAlignment = VerticalAlignment.Center; tb.KeyUp += new KeyEventHandler(textBox_keyDown); tb.IsEnabled = AutoBoxIsEnable; IsAddButtonInTheFirst = isAddButton; } #endregion #region Methods private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { } private void textBox_keyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Down) { if (lb.HasItems && lb.SelectedIndex < lb.Items.Count) { lb.SelectedIndex = lb.SelectedIndex + 1; lb.ScrollIntoView(lb.SelectedItem); } } if (e.Key == Key.Up && lb.SelectedIndex > 0) { if (lb.HasItems) { lb.SelectedIndex = lb.SelectedIndex - 1; lb.ScrollIntoView(lb.SelectedItem); } } if (e.Key == Key.Enter && lb.SelectedItem != null) { if (IsAddButtonInTheFirst && lb.SelectedIndex < 1) { return; } tb.Text = (SelectListItem as ListBoxItem).Content.ToString(); tb.Focus(); if (tb.Text.Trim().Length > 0) { tb.Select(tb.Text.Trim().Length, 0); } if (lb.HasItems) { lb.Items.Clear(); } } } private void TextChanged() { #region lb.ItemsSource = null; lb.SelectionChanged -= new SelectionChangedEventHandler(listBox_SelectionChanged); if (selectList != null) { selectList.Clear(); } if (tb.Text.Length >= searchThreshold) { SelectList = new ObservableCollection<AutoCompleteEntry>(autoCompletionList.Where(p => p.Text.Contains(tb.Text.Trim()))); if (SelectList.Count > 0) { if (!IsAddButtonInTheFirst) { lb.ItemsSource = selectList; // lb.DisplayMemberPath = "Text"; lb.Visibility = Visibility.Visible; } else { AutoCompleteEntry temp = new AutoCompleteEntry(); temp.BtnVisibility = Visibility.Visible; temp.ImgVisibility = Visibility.Visible; temp.ButtonContent = "添加"; selectList.Insert(0, temp); lb.ItemsSource = selectList; // lb.DisplayMemberPath = "Text"; lb.Visibility = Visibility.Visible; } } else lb.Visibility = Visibility.Collapsed; } else { lb.Visibility = Visibility.Collapsed; } lb.SelectionChanged += new SelectionChangedEventHandler(listBox_SelectionChanged); #endregion } private void textBox_TextChanged(object sender, TextChangedEventArgs e) { if (insertText == true) insertText = false; else { TextChanged(); } } #endregion private void tb_LostFocus(object sender, RoutedEventArgs e) { if (!lb.IsFocused) { lb.Visibility = Visibility.Collapsed; } } private void Button_Click(object sender, RoutedEventArgs e) { } } }
<UserControl x:Class="WpfApplication1.autoBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <UserControl.Resources> <DataTemplate x:Key="listboxtemplate"> <StackPanel> <Image Source="{Binding}" Visibility="{Binding ImgVisibility}" Width="16" Height="16" Stretch="Fill" Margin="2"></Image> <Button Content="{Binding ButtonContent}" Visibility="{Binding BtnVisibility}" Foreground="Blue" Click="Button_Click" /> <TextBlock Text="{Binding Path=Text}" Margin="2" ></TextBlock> </StackPanel> </DataTemplate> </UserControl.Resources> <StackPanel> <TextBox Name="tb" LostFocus="tb_LostFocus"/> <ListBox Name="lb" MaxHeight="150" Canvas.Left="0" Canvas.Top="20" Visibility="Visible" ItemTemplate="{StaticResource listboxtemplate}"> </ListBox> </StackPanel> </UserControl>
上面的就是代码,主要是button的事件不响应,求教。
@翻墙小龙虾: 把click换成PreviewMouseDown/PreviewMouseUp 这两个的一个事件试试吧。
@荒野的呼唤: 这两个可以,click不行,好奇怪。初学不是很懂,为什么会有这种情况啊?
@翻墙小龙虾: 因为click有可能有别的元素执行这个事件。
@荒野的呼唤: 感谢