wpf 怎么点击按钮让listbox新增一列
答案:1 悬赏:80 手机版
解决时间 2021-02-25 05:52
- 提问者网友:抽煙菂渘情少年
- 2021-02-24 21:57
wpf 怎么点击按钮让listbox新增一列
最佳答案
- 五星知识达人网友:長槍戰八方
- 2021-02-24 22:48
自定义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());
}
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());
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯