wpf 怎么点击按钮让listbox新增一列
展开全部
自定义ListBox类
public class myListBox : System.Windows.Controls.ListBox
{
protected override DependencyObject GetContainerForItemOverride()
{
return new myListBoxItem();
}
}
public class myListBoxItem : System.Windows.Controls.ListBoxItem
{
protected override void OnSelected(System.Windows.RoutedEventArgs e)
{
DependencyObject dep = (DependencyObject)e.OriginalSource;
while ((dep != null) && !(dep is ListBoxItem))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return;
ListBoxItem item = (ListBoxItem)dep;
if (item.IsSelected)
{
item.IsSelected = !item.IsSelected;
//e.Handled = true;
}
base.OnSelected(e);
}
}
页面引用
xmlns:control="clr-namespace:wpf.DependencyControl"
// 在Grid 中写
<control:myListBox x:Name="myListBox" Width="100" Height="100" SelectionMode="Single"
SelectionChanged="myListBox_SelectionChanged">
// 后台cs代码
private void myListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
object o = myListBox.SelectedItem;
if (o == null)
return;
MessageBox.Show(o.ToString());
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询