我希望ListItems以其橙色背景扩展为Listbox的整个宽度.
目前它们只与FirstName + LastName一样宽.
我已将每个元素设置为:HorizontalAlignment ="Stretch".
我希望ListboxItems的背景随着用户拉伸列表框而扩展,因此我不想输入绝对值.
我需要做什么才能使ListBoxItems的背景颜色填充ListBox的宽度?
<Window x:Class="TestListBoxSelectedItemStyle.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestListBoxSelectedItemStyle"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<local:CustomerViewModel x:Key="TheDataProvider"/>
<DataTemplate x:Key="CustomerItemTemplate">
<Border CornerRadius="5" Background="Orange" HorizontalAlignment="Stretch" Padding="5" Margin="3">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" Width="Auto">
<TextBlock HorizontalAlignment="Stretch">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="FirstName"/>
<Binding Path="LastName"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</Border>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{Binding Path=GetAllCustomers, Source={StaticResource TheDataProvider}}"
ItemTemplate="{StaticResource CustomerItemTemplate}"/>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud) 注意我已经问过相关问题:如何组合DataTrigger和EventTrigger?
我有一个包含几个项目的列表框.该项的类实现INotifyPropertyChanged
并具有属性IsAvailable
.我使用该属性来指示列表中使用不同颜色的不可用选项.
但是,如果所选项目不可用,则前景色应为红色.
<ListBox>
<ListBox.Resources>
<DataTemplate DataType="{x:Type local:InstitutionViewModel}">
<TextBlock Name="Name" Text="{Binding Name}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsAvailable}" Value="False">
<Setter TargetName="Name" Property="Foreground" Value="#888"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.Resources>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
我使用上面的数据触发器来灰显不可用的项目.
我面临的问题是,选择项目的事实与模板绑定的基础数据无关.我真正想要的是某种多触发器,它支持Trigger
依赖属性(ListBoxItem.IsSelected
)上的常规以及DataTrigger
绑定数据项.
可以在不将选择概念引入我的视图模型的情况下完成吗?
对于任何想知道我为什么不禁用不可用项目的人,请理解应用程序的要求是可以选择不可用的选项.实际上有几个列表框,其中一个选项会影响其他列表框中的可用内容.我无法禁用这些项目,因为如果根据之前的选择禁用了项目,用户将无法改变主意或探索不同的组合.
我有一个包含一些项目的列表框.无论如何我可以在每个项目上附加双击事件吗?
Item 1
Item 2
Item 3
Run Code Online (Sandbox Code Playgroud)
如果我要双击第2项,将弹出一个消息框,说"第2项"
我该怎么办?
我ListBox
在我的wpf窗口中绑定了一个ObervableCollection
.如果有人点击了某个元素ListBox
(就像链接一样),我想打开浏览器.谁能告诉我怎么做?我发现了listboxviews的一些东西,它只是以这种方式工作还是有一种方法只是使用ListBox
?
你的
塞巴斯蒂安
我希望在ListBox中的某个项被鼠标单击时得到通知,无论它是否已被选中.
我搜索并发现了这个:(http://kevin-berridge.blogspot.com/2008/06/wpf-listboxitem-double-click.html查看评论)
private void AddDoubleClickEventStyle(ListBox listBox, MouseButtonEventHandler mouseButtonEventHandler)
{
if (listBox.ItemContainerStyle == null)
listBox.ItemContainerStyle = new Style(typeof(ListBoxItem));
listBox.ItemContainerStyle.Setters.Add(new EventSetter()
{
Event = MouseDoubleClickEvent,
Handler = mouseButtonEventHandler
});
}
//Usage:
AddDoubleClickEventStyle(listView1, new MouseButtonEventHandler(listView1_MouseDoubleClick));
Run Code Online (Sandbox Code Playgroud)
这是有效的,但它是为了它DoubleClick
.虽然我不能让它工作一次.我试过MouseLeftButtonDownEvent
- 因为似乎没有MouseClick
事件,但它没有被调用.
一个更普遍的侧面问题:我怎样才能看到哪些事件确实存在,哪些处理程序与它们对应以及何时实际执行某些操作?例如,什么告诉我,MouseDoubleClickEvent
我需要一个MouseButtonEventHandler
?也许对于MouseLeftButtonDownEvent
我需要一些其他处理程序,这就是为什么它不起作用?
我也尝试了子类化ListBoxItem
和覆盖OnMouseLeftButtonDown
- 但它也没有被调用.
渣
我创造了以下事件ListBox
:
<ListBox x:Name="RecentItemsListBox" Grid.Row="1" BorderThickness="0" Margin="2,0,0,0" SelectionChanged="RecentItemsListBox_SelectionChanged">
<ListBox.Resources>
<Style TargetType="{x:Type ListBoxItem}"
BasedOn="{StaticResource {x:Type ListBoxItem}}">
<Style.Triggers>
<!--This trigger is needed, because RelativeSource binding can only succeeds if the current ListBoxItem is already connected to its visual parent-->
<Trigger Property="IsVisible" Value="True">
<Setter Property="HorizontalContentAlignment"
Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
<Setter Property="VerticalContentAlignment"
Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,2,0,0">
<TextBlock Text="{Binding Number}" />
<StackPanel Orientation="Vertical" Margin="7,0,0,0">
<TextBlock Text="{Binding File}" />
<TextBlock Text="{Binding Dir}" …
Run Code Online (Sandbox Code Playgroud) 我有一个WPF ListBox
控件,我将其设置ItemsSource
为项目对象的集合.如何将对象的IsSelected
属性绑定ListBoxItem
到Selected
相应项对象的属性,而不将对象的实例设置为Binding.Source
?
我知道:
String test = "test";
ListBox.Items.Add(test);
Run Code Online (Sandbox Code Playgroud)
要么
String test = "test";
int index = 1;
ListBox.Items.Insert(index, String);
Run Code Online (Sandbox Code Playgroud)
在ListBox中添加String,但我想插入ListBoxItem,如何?以前我学到了
var contentToString = (String)ListBoxItem.Content;
Run Code Online (Sandbox Code Playgroud)
只是将ListBoxItem转换为String,但我无法将String转换为ListBoxItem
在我的WPF应用程序中,我正在处理ListBox SelectionChanged事件,它运行正常.
现在我需要处理一个点击事件(即使对于已经选择的项目); 我尝试过MouseDown,但它不起作用.如何处理ListBox单击项目?
我需要一个在第一次单击时选择的列表框,在第二次单击时取消选择,这样在任何时候都只选择零个或一个项目.
当您按住crtl时,选择/取消选择在列表框中实现(使用SelectionMode ="Single"),但不幸的是,我的所有用户都不可能知道这一点.
使用SelectionMode ="Multiple",我们拥有我想要的确切功能,除了您可以选择多个项目...
更多背景:我希望用户首先选择要登录的安装,然后提供凭据(以及其他一些选择)
为实现这一目标,我使用了一个扩展内容的列表框.为了帮助扩展,我在listboxitem的左侧创建了一个三角形,当你选择了列表框项目时,当未展开时指向右转.
因此,首先用户首先看到安装列表,然后,当他通过选择它选择了他想要的项目时,listboxitem会扩展到他需要输入的其他信息.它非常好,而且运行良好,但测试报告他们希望第二次点击三角形以取消选择(从而折叠展开的部分).我必须承认,我也点击了¤%&箭头,期望行动导致崩溃... :-(
任何人都知道如何实现这一目标(最好没有代码)?
listboxitem ×10
wpf ×8
listbox ×6
c# ×3
xaml ×3
.net ×2
events ×2
selection ×2
binding ×1
click ×1
data-binding ×1
double-click ×1
selected ×1
string ×1
triggers ×1