很抱歉,如果这是一个基本问题,但我如何获取ListBox的ItemTemplate,并将其放在窗口的资源中,以便多个ListBox可以使用它.
这是一些XAML:
<Window x:Class="Example">
<Window.Resources>
<DataTemplate x:Key="dtExample">
<ListBox.ItemTemplate>
// styles go here...
</ListBox.ItemTemplate>
</DataTemplate>
</Window.Resources>
<ListBox ItemTemplate="{StaticResource dtExample}">
// items go here...
</ListBox>
</Window>
Run Code Online (Sandbox Code Playgroud)
这是一个"附加属性没有设置者"设计时错误.为了简洁起见,我删除了一些我认为不重要的代码.
谢谢
我正在使用带有ItemTemplate的Listbox,当我从列表中选择一个项目时,它显示蓝色背景我如何设置所选项目的样式,类似于未选择的项目?
我有一个元素集合,每个元素都有一个名称和一个图像blob的子集合.我想显示一个Accordion,每个项目代表每个MainElements.在每个元素内部,我在所述MainElement的子集合中显示图像.Accordion会被用户调整大小,因此我使用了一个wrappanel来呈现图像.当手风琴足够宽时,图像重新排序,每个排列的数量与每行中的可能性一致.当wrappanel每行只显示一个图像时会出现问题(因为没有足够的空间可供更多),图像列表会继续,但我看不到所有图像,因为它们不适合控件的高度.我需要在AccordionItem内显示一个垂直滚动条,以便我可以向下滚动图像列表.所以,这是我的代码:
<layoutToolkit:Accordion Width="Auto" Height="Auto" ItemsSource="{Binding MainElementCollection}">
<layoutToolkit:Accordion.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding MainElementName}" />
</DataTemplate>
</layoutToolkit:Accordion.ItemTemplate>
<layoutToolkit:Accordion.ContentTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding SubElementCollection}" ScrollViewer.VerticalScrollBarVisibility="Auto" >
<ItemsControl.Template>
<ControlTemplate>
<controlsToolkit:WrapPanel />
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Image Margin="2" Width="150" Source="{Binding PreviewImage, Converter={StaticResource ImageConverter}}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</layoutToolkit:Accordion.ContentTemplate>
</layoutToolkit:Accordion>
Run Code Online (Sandbox Code Playgroud)
http://www.silverlightshow.net/tips/How-to-add-scrollbars-to-ItemsControl.aspx建议我应该用一个scrollviewer环绕我的wrappanel,就像这样
<ItemsControl.Template>
<ControlTemplate>
<scrollviewer>
<controlsToolkit:WrapPanel />
</scrollviewer>
</ControlTemplate>
</ItemsControl.Template>
Run Code Online (Sandbox Code Playgroud)
但是后来我的包装变得非常小,我只能看到一个小的垂直滚动条任何想法?非常感谢.
编辑:我认为当在controltemplate中使用时,wrappanel会失去其宽度
它应该如下使用:
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<controlsToolkit:WrapPanel ScrollViewer.VerticalScrollBarVisibility="Visible" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
Run Code Online (Sandbox Code Playgroud)
无论如何,我尝试添加ScrollViewer.VerticalScrollBarVisibility ="Visible"行,但我又被卡住了.
再次编辑:
现在我的包装袋看起来像这样:
<ItemsControl ItemsSource="{Binding StageVideos}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<controlsToolkit:WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate> …Run Code Online (Sandbox Code Playgroud) silverlight wrappanel itemtemplate itemscontrol scrollviewer
我试图在TreeView中更改SelectedItem模板.我按照描述在Style.Triggers中编写了简单的容器样式并更改了项目模板
[1]:如何使用某种颜色突出显示树视图所选项目?要么
[2]:WPF TreeView:如何设置带有圆角的选定项目,如在资源管理器中,但它不起作用.
然后我创建了一个新项目,并使用简单的样式和模板创建了TreeView
<TreeView>
<TreeViewItem Header="Item1" />
<TreeViewItem Header="Item2" />
<TreeViewItem Header="Item3"/>
<TreeView.Resources>
<DataTemplate DataType="{x:Type TreeViewItem}" x:Key="selectedTemplate">
<StackPanel Height="25">
<TextBlock Text="SelectedItem"/>
</StackPanel>
</DataTemplate>
</TreeView.Resources>
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontStyle" Value="Italic"/>
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Background" Value="Green"/>
<Setter Property="ItemTemplate" Value="{StaticResource selectedTemplate}"/>
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
Run Code Online (Sandbox Code Playgroud)
所以,然后我在树视图中选择了TreeViewItem,FontWeight,FontStyle和Foreground都被更改了,但是Background和ItemTemplate没有改变.
结果: 
你能解释这个奇怪的行为吗?
所以我在使用按钮DataTemplate我的Listbox ItemTemplate.任何想法如何Listbox从按钮点击获取项目的索引?我看不到抓住按钮的父母.
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type local:Img}">
<Button Click="lstButton_Click">...
Run Code Online (Sandbox Code Playgroud) 我有一个 WrapPanel,我正在用 UserControls 填充我的 ViewModel:
<WrapPanel Width="250" Orientation="Horizontal" Margin="3">
<ItemsControl ItemsSource="{Binding PlaceableObjectsContent}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type local:PlaceableObjectViewModel}">
<local:PlaceableObjectUserControl>
<local:PlaceableObjectUserControl.InputBindings>
<MouseBinding MouseAction="LeftClick"
Command="{Binding DataContext.OnPlaceableObjectClicked, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
CommandParameter="{Binding}"/>
</local:PlaceableObjectUserControl.InputBindings>
</local:PlaceableObjectUserControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</WrapPanel>
Run Code Online (Sandbox Code Playgroud)
当我用随机控件填充它们时,一切正常!我已经阅读了一些关于由于使用 ItemTemplate 的问题!?如果是真的,我该如何管理?
谢谢
让我用伪代码问这个问题:
__PRE__
其中
"Contacts"将ViewModel对象设置为窗口的DataContext.
"Contacts"具有"PersonCollection",公共ICommand PersonSelectedCommand属性."PersonCollection"是List
"Person"具有Name,Age属性
目前这不起作用,因为CheckBox试图找到并绑定对象"person"的ICommand"PersonSelectedCommand"属性,该属性不存在!
如何将CheckBox绑定到对象"Contact"的ICommand"PersonSelectedCommand"属性
谢谢并问候
123Deveopler
我有一个转发器,每一行都有一个图像按钮.
通过单击每个图像按钮,我应该获得相应行的Id,但我不知道如何管理转发器中此图像按钮的单击事件.
我有以下ListView(简化):
<ListView Name="lvwNotes" KeyUp="lvwNotes_KeyUp">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<DockPanel Background="LightGray">
<TextBlock DockPanel.Dock="Right" Text="{Binding Path=Author}" />
<TextBlock Text="{Binding Path=Timestamp}" />
</DockPanel>
<TextBox Text="{Binding Path=Text}"
GotFocus = "lvwNotes_TextBox_GotFocus"
TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)
通过单击更改所选项目仅在用户使用TextBlocks单击DockPanel时有效,但在单击TextBox时则无效.我想要实现的是将所选项目设置为包含用户单击的TextBox的项目.
我设法通过与TextBox相关的ListViewItem:
private void lvwNotes_TextBox_GotFocus(object sender, RoutedEventArgs e) {
DependencyObject o = Tools.GetAncestorByType((DependencyObject)sender, typeof(ListViewItem));
if (!o.Equals(null)) {
// code to select this ListViewItem
}
}
Run Code Online (Sandbox Code Playgroud)
但是设定
lvwNotes.SelectedIten = o ;
Run Code Online (Sandbox Code Playgroud)
仍然没有效果.我也尝试过Dispatcher.BeginInvoke的一些技巧,但说实话,我并不确切知道我在那里做什么.
我有一个ItemsCountrol,它的ItemsSource属性绑定到ObservableCollection。我有一个显示此类型的用户控件(TeamUserControl)。我创建了一个数据模板,用于为itemssource集合中的每个customtype项加载此用户控件。此时,我在TeamUserControl中所做的任何Binding语句都可以直接通过路径{Binding Path = TeamOwner}引用Team属性并开始工作。有没有一种方法可以将引用绑定到用户控件代表的ItemsSource项目?例如,在TeamUserControl中创建Team类型的依赖项属性,并将其绑定到ItemsSource中的项目实例。
<ItemsControl Name="ItemCtrl" Grid.Row="0" ItemsSource="{Binding Path=League.Teams}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<mycontrols:TeamUserControl AttachedTeam="{Binding ???}" TeamOwnerName="{Binding Path=TeamOwner}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
在此示例中,窗口表示具有属性:ObservableCollection Teams的“联盟”类。并且有一个类“ Team”,其属性为:TeamOwner。TeamUserControl具有两个从属属性:Team类型的AttachedTeam和string类型的TeamOwnerName。
我包括了team-owner属性引用,以显示每个这些用户控件都有一个Team实例。我只是不确定如何引用它。
我正在尝试将WPF组合框绑定到可观察的图像集合.这是我的收藏:
public class AvatarPhoto
{
public int AvatarId { get; set; }
public BitmapImage AvatarImage { get; set; }
}
public ObservableCollection<AvatarPhoto> AvailableProfilePictures { get; private set; }
Run Code Online (Sandbox Code Playgroud)
这是我的xaml:

Visual Studio给出了这个编译时错误:属性'ItemTemplate'不支持'Image'类型的值.
为什么会出现这个错误?
谢谢
更新:谢谢你的回答!它解决了这个问题.
现在我已经更新了我的代码但是我在ComboBox中看到了这个:

为什么不能正确显示图片?在调试窗口中,我可以看到我的集合已正确填充:

itemtemplate ×11
wpf ×9
binding ×2
c# ×2
itemscontrol ×2
listbox ×2
selecteditem ×2
wrappanel ×2
asp.net ×1
combobox ×1
datatemplate ×1
icommand ×1
image ×1
itemssource ×1
listview ×1
repeater ×1
scrollviewer ×1
silverlight ×1
textbox ×1
treeview ×1
triggers ×1
xaml ×1