我想要实现以下目标:
<Style TargetType="ListBoxItem">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Name="mnuEdit" Header="_Edit" Click="MenuItem_Click" />
</ContextMenu>
</Setter.Value>
</Setter>
<Style>
Run Code Online (Sandbox Code Playgroud)
但它抛出以下异常:
Cannot add content of type 'System.Windows.Controls.ContextMenu'
to an object of type 'System.Object'.
Error at object 'System.Windows.Controls.ContextMenu'
in markup file blah blah blah
Run Code Online (Sandbox Code Playgroud) 这就是我想做的事情.我从数据库中获取对象列表,并将此列表绑定到ListBox控件.ListBoxItems包含一个文本框和一个按钮.这就是我想出的.到目前为止,它按预期工作.该对象有许多属性,如ID,Name.如果我单击ListBoxItem中的按钮,则应该从ListBox和数据库中删除Item ...
<ListBox x:Name="taglistBox">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter HorizontalAlignment="Stretch"/>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Tag" Value="{Binding TagSelf}"></Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Name="btTag" VerticalAlignment="Center" Click="btTag_Click" HorizontalAlignment="Left">
<Image Width="16" Height="16" Source="/WpfApplication1;component/Resources/104.png"/>
</Button>
<TextBlock Name="tbtagBoxTagItem" Margin="5" Grid.Column="1" Text="{Binding Name}" VerticalAlignment="Center" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
Textblock.Text绑定到object.Name,ListBoxItem.Tag绑定到object.TagSelf(它只是对象本身的副本).
现在我的问题
如果我单击listboxItem中的按钮,我如何获取listboxitem以及绑定到它的对象.为了从数据库中删除对象,我必须以某种方式检索它.我试过类似的东西
ListBoxItem lbi1 =
(ListBoxItem)(taglistBox.ItemContainerGenerator.ContainerFromItem(taglistBox.Items.CurrentItem)); ObjectInQuestion t =(ObjectInQuestion)lbi1.Tag;
如果Itemssource更改,有没有办法自动更新ListBox的内容?现在我正在实现这个目标
taglistBox.ItemsSource = null;
taglistBox.ItemsSource = ObjectInQuestion;
我很感激您能给予的任何帮助:D提前致谢
我有一个为我定义的样式ListBoxItems,触发器设置IsSelected为True 时的背景颜色:
<Style x:Key="StepItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" Padding="0" SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="#40a0f5ff"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
这种风格即使在ListBox和ListBoxItem失去焦点时也能保持所选项目,在我的情况下绝对是必须的.问题是我还希望ListBoxItem在其中一个TextBox孩子得到关注时被选中.为了达到这个目的,我添加了一个触发器,IsSelected当IsKeyboardFocusWithin为true 时设置为true:
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True" />
</Trigger>
Run Code Online (Sandbox Code Playgroud)
当我添加此触发器时,焦点在子节点上时会选择Item TextBox,但第一个行为会消失.现在当我点击外面时ListBox,该项目被取消选中.
我怎样才能保持这两种行为?
当用户点击ListBoxItem时,我希望它是一个大胆的大字体红色背景黄色
一切都有效,除了背景.似乎所选项目有标准(蓝色)背景.如何覆盖它并将所选背景更改为黄色?
这是代码:
<Window x:Class="AlternateListBox2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
xmlns:local="clr-namespace:AlternateListBox2">
<Window.Resources>
<local:Countries x:Key="countries"/>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Content" Value="{Binding Path=Name}"/>
<Setter Property="Margin" Value="2"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontSize" Value="18"/>
<Setter Property="Background" Value="Yellow"/>
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<ListBox
ItemsSource="{Binding Source={StaticResource countries}}"
Width="100"
Margin="10"
HorizontalAlignment="Left"
/>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud) 我有一个ListBox.现在我想以这种方式编写一个DataTemplate,第一个项目将具有红色背景和其他项目的白色背景.我想我需要编写一个DataTrigger,但我不知道如何确定DataTemplate是否适用于第一个项目.
如何提取 ListBoxItem 的父容器?在下面的例子中,我可以去到 ListBoxItem,比它更高,我什么都没有:
<ListBox Name="lbAddress">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Click="Button_Click"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
Private Sub Button_Click(sender As Button, e As RoutedEventArgs)
Dim lbAddress = GetAncestor(Of ListBox) 'Result: Nothing
End Sub
Run Code Online (Sandbox Code Playgroud)
Public Shared Function GetAncestor(Of T)(reference As DependencyObject) As T
Dim parent = GetParent(reference)
While parent IsNot Nothing AndAlso Not parent.GetType.Equals(GetType(T))
parent = GetAncestor(Of T)(parent)
End While
If parent IsNot Nothing Then _
Return If(parent.GetType Is GetType(T), parent, Nothing)
Return Nothing
End Sub
Public Function GetParent(reference As DependencyObject) As DependencyObject
Dim …Run Code Online (Sandbox Code Playgroud) 我正在使用WPF .net 4.5(c#),我想创建一个包含一系列用户控件的ListBox.(如果列表框是错误的控件类型,请告诉我).
我希望我的列表框将用户控件的副本作为列表项,每个列表项中包含不同的内容.
如何将用户控件添加到列表框?
感谢您的帮助!
我有一个ListBox定制项目的风格。我希望该项目在选择时变大一点,并在取消选择时恢复到其原始大小。我尝试了几种解决方案,但似乎都不起作用。我认为问题在于 的正确设置Storyboard.TargetProperty。
我当前的 XAML 如下所示:
...
<ListBox.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="LayoutTransform.(ScaleTransform.ScaleX)" To="1.2" Duration="0:0:.3" AutoReverse="True"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Resources>
...
Run Code Online (Sandbox Code Playgroud)
我的最终代码(已应用答案):
...
<ListBox.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="RenderTransformOrigin" Value="0.5,0.5" />
<Setter Property="RenderTransform">
<Setter.Value>
<ScaleTransform />
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleX" To="1.1" Duration="0:0:.1" />
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleY" To="1.1" Duration="0:0:.1" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleX" To="1.0" Duration="0:0:.1" />
<DoubleAnimation …Run Code Online (Sandbox Code Playgroud) 我确信这很简单,但我似乎无法弄清楚如何做到这一点.基本上我有一个来自天蓝色移动服务数据库的客户列表.到目前为止一切正常但我想根据数据为列表框中的每个项目设置项目模板.我有两个模板,一个用于公司,一个用于一个人.我的问题是如何应用每一个.
模板
<DataTemplate x:Key="CompanyItemTemplate">
-------
</DataTemplate>
<DataTemplate x:Key="CustomerItemTemplate">
-------
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
码
CustomerListItems.ItemsSource = customeritems.OrderBy(customer => customer.CustomerName);
foreach (Customers customer in customeritems)
{
if (customer.Company != "")
{
CustomerListItems.ItemTemplate = CompanyItemTemplate;
}
else
{
CustomerListItems.ItemTemplate = CustomerItemTemplate;
}
}
Run Code Online (Sandbox Code Playgroud) 我创建了一个自定义 ListBox,每个项目由分隔符分隔。但我看到了奇怪的问题。分隔符的粗细在列表项中不是恒定的。如果我更改列表框的位置,如此List Box Image所示,它会发生变化 。
下面是自定义列表框的源代码。
<Window x:Class="CustListBox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustListBox"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:Manager x:Key="manager"/>
<Style x:Key="LstStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<StackPanel>
<ContentPresenter/>
<Separator Foreground="Gray"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<ListBox Name="CustListBox" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Source={StaticResource manager}, Path=UserList}" ItemContainerStyle="{Binding Source={StaticResource LstStyle}}" Margin="26,17,271,27">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=FirstName}"/>
<TextBlock Text="{Binding Path=SecondName}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox Height="278" HorizontalAlignment="Left" Margin="264,16,0,0" Name="listBox1" VerticalAlignment="Top" Width="218" ItemsSource="{Binding Source={StaticResource manager}, Path=Names}" ItemContainerStyle="{Binding Source={StaticResource LstStyle}}"/>
</Grid>
</Window>
using …Run Code Online (Sandbox Code Playgroud) listboxitem ×10
wpf ×9
listbox ×7
c# ×3
animation ×1
background ×1
contextmenu ×1
data-binding ×1
datatemplate ×1
datatrigger ×1
setter ×1
styles ×1
triggers ×1
visual-tree ×1
windows-8.1 ×1
winrt-xaml ×1
xaml ×1