源:stackoverflow--Listbox模板内按钮点击命令指定到挡墙vm内命令Command="{Binding DataContext.PlusCmd,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}"转载于:https://www.cnblogs.com/oiliu/...
object obj = new object();
int i = 0;
private void
Button
_Click(object sender, RoutedEventArgs e)
this.IsEnabled = false;
var t = (DateTime.Now - lastClick).TotalMilliseconds;
lastClick = Dat
今天碰到一个需求,使用
ListBox
显示多文字。因为每个选项文字较长,
ListBoxItem
的宽度有限,这时候为了体现界面友好,增加ToolTip是一个好的选择:代码如下:
MainWindow.xmal:
<Window x:Class="_20200116_MVVM.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http...
在View中为
ListBoxItem
设置PreviewMouseLeft
Button
Down事件,并为IsSelected属性设置绑定。
<UserControl x:Class="_5_RoutedEventTest.Views.
ListBox
Test"
xmlns="htt...
最近在项目中使用
ListBox
时遇到了这样的问题:
ListBox
中有一个删除按钮,点
击
后要删除
当前
项;翻资料后发现可以这样处理:
在
Button
的点
击
事件中通过
var curItem = ((
ListBoxItem
)video_name.ContainerFromElement((System.Windows.Controls.
Button
)sender)).Content;
有人可能会说这有什么好写的。不就是一行代码就能搞定的吗?而且为什么需要用代码设置SelectedItem呢?用户所点的Item不就自动是SelectedItem吗?在这里将要讨论我们的,就是
ListBox
自己没有能自己把SelectedItem设置正确的情况。本来想当作一个
WPF
Bug清单的一篇文章的,但是又感觉也许就是有这样变态的需求呢。
我们用一个非常简单的代码的XAML就可以重现这...
<Window x:Class="
Wpf
App2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml.
<Style x:Key="ThumbItemStyle" TargetType="{x:Type
ListBoxItem
}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type
ListBoxItem
}">
<
ListBox
Name="my
ListBox
">
<
ListBoxItem
>Item 1</
ListBoxItem
>
<
ListBoxItem
>Item 2</
ListBoxItem
>
<
ListBoxItem
>Item 3</
ListBoxItem
>
</
ListBox
>
2. 在代码中向
ListBox
中添加数据项。
```csharp
my
ListBox
.Items.Add("Item 4");
my
ListBox
.Items.Add("Item 5");
3. 通过数据绑定将数据源与
ListBox
关联。
```csharp
List<string> items = new List<string> { "Item 6", "Item 7", "Item 8" };
my
ListBox
.ItemsSource = items;
4. 处理
ListBox
的选择事件。
```csharp
private void my
ListBox
_SelectionChanged(object sender, SelectionChangedEventArgs e)
if (my
ListBox
.SelectedItem != null)
MessageBox.Show(my
ListBox
.SelectedItem.ToString());
这些是
WPF
ListBox
的基本用法。您还可以自定义
ListBox
的外观和行为,如设置选项
模板
,指定选项容器样式等等。