在WPF中Listbox,我很困惑与这些概念2:
ItemTemplate和ItemContainerStyle
一个人能更给我解释一下?
我正在尝试将一个集合绑定到ItemsControl,将Canvas作为items面板,并将每个项目的Canvas.Left和Top绑定到项目对象上的属性.基本上我正在尝试重新创建我在博客上的这篇文章中描述的二维数据绑定,但这次是在WinRT而不是WPF.
由于ItemsControl将您的ItemTemplate内容包装在另一个UI元素(在WinRT的情况下为ContentPresenter)中,并且它是直接放置在items面板中的那些包装器/容器元素,因此必须在这些容器上设置Left和Top; 你不能只在DataTemplate中设置它们.在WPF中,使用ItemContainerStyle中的绑定很容易做到这一点,例如:
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Left" Value="{Binding Path=X}"/>
<Setter Property="Canvas.Top" Value="{Binding Path=Y}"/>
</Style>
</ItemsControl.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)
但是当我在WinRT/XAML项目中尝试同样的事情时,我什么也得不到.甚至没有绑定错误.如果我对一个值进行硬编码,它就会起作用; 但是如果我使用绑定,则该属性将保持其默认值(零),并且"输出"窗口中不显示任何绑定错误.
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<!-- This works, so ItemContainerStyle does work in WinRT: -->
<Setter Property="Canvas.Left" Value="200"/>
<!-- But this silently fails, leaves Top as 0, and does not show
any binding errors in the debugger's Output window: -->
<Setter Property="Canvas.Top" Value="{Binding Y}"/>
</Style>
</ItemsControl.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)
我已经验证了ContentPresenters确实有正确的DataContext(即集合项,而不是集合本身或其他时髦的东西),所以你认为这些绑定可以正常工作.但他们似乎甚至没有得到评估.如果我在其他地方放置了一个错误的绑定,并运行调试版本,我会在调试器的Output窗口中看到绑定错误; 但是如果我在ItemContainerStyle中引用了一个无意义的属性,则不会显示绑定错误.
这是一个更完整的例子,(据我所知)应该可以在WPF中正常工作,但是在WinRT中,所有内容都保留在原点:
<ItemsControl ItemsSource="{Binding Tiles}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style …Run Code Online (Sandbox Code Playgroud) binding itemscontrol itemcontainerstyle windows-runtime winrt-xaml
以下类似于我想要完成的任务.但是,我得到了错误
PropertyDescriptor值无效.
在模板上Setter.我怀疑这是因为我没有指定TargetType的Style; 但是,我不知道容器的类型ItemsControl.
<ItemsControl>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<StackPanel>
<TextBlock Text="Some Content Here" />
<ContentPresenter />
<Button Content="Edit" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ItemsControl.ItemContainerStyle>
<!-- heterogenous controls -->
<ItemsControl.Items>
<Button Content="Content 1" />
<TextBox Text="Content 2" />
<Label Content="Content 3" />
</ItemsControl.Items>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud) 我有创建xaml控件的问题.我正在使用通用应用程序在VS 2015中编写新项目.我想创建网格.在这个网格中我想要一个按钮.在模型中,我指定了列(Level)和Row.这是我的代码:
<ItemsControl Grid.Row="1" ItemsSource="{Binding Path=TechnologyList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="10*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="10*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="14*"/>
<ColumnDefinition Width="14*"/>
<ColumnDefinition Width="14*"/>
<ColumnDefinition Width="14*"/>
<ColumnDefinition Width="14*"/>
<ColumnDefinition Width="14*"/>
<ColumnDefinition Width="14*"/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="Control">
<Setter Property="Grid.Column" Value="{Binding Level}" />
<Setter Property="Grid.Row" Value="{Binding Row}" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Name}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
我遇到错误<Setter Property="Grid.Column" Value="{Binding Level}" />
错误:HRESULT的异常:0x8000FFFF(E_UNEXPECTED)在edytor中没有运行代码.怎么了?在"旧"WPF中一切都很好,但在Universal …
我已经看过其他一些Silverlight'vs'问题了,但找不到任何特别的比赛.
我正在尝试定义我的对象绑定到的方式ListBox将显示.我已经定义了一个DataTemplate,但我真的不确定它应该在哪里结束并且ItemContainerStyle应该开始.
问题1:
这ItemContainerStyle只是一个包装器,DataTemplate以便可以将一个共同的项目样式应用于不同的数据布局吗?
问题1a:如果是这样,如果不需要通用的项目样式,ItemContainerStyle甚至是必要的还是可以在DataTemplate?中定义所有的布局和样式?
问题1b:如果没有,那么它是什么?
在ListBox目前这样的:
<ListBox Margin="40,118,41,61" ItemTemplate="{StaticResource TaskDataTemplate}"/>
Run Code Online (Sandbox Code Playgroud)
我的XAML DataTemplate是这样的:
<DataTemplate x:Key="TaskDataTemplate">
<Grid d:DesignHeight="95" Height="150">
<StackPanel Margin="11,8,-10,68" Orientation="Horizontal" d:LayoutOverrides="Width">
<TextBlock x:Name="TaskLabel" Margin="0,0,0,8" Style="{StaticResource TitleTextSmall}" TextWrapping="Wrap" Text="Task" VerticalAlignment="Stretch" d:LayoutOverrides="Height"/>
<TextBlock x:Name="TaskID" HorizontalAlignment="Right" Margin="10,0,0,0" Style="{StaticResource TitleTextSmall}" TextWrapping="Wrap" Text="TaskID" VerticalAlignment="Stretch" d:LayoutOverrides="Height"/>
<TextBlock x:Name="ChangeList" Style="{StaticResource NormalText}" TextWrapping="Wrap" Text="Changes..." Margin="30,2,0,0"/>
</StackPanel>
<ComboBox x:Name="TaskType" Style="{StaticResource TaskComboBox}" Height="29" VerticalAlignment="Top" Margin="131,30,16,0" d:LayoutOverrides="VerticalAlignment"/>
<TextBlock x:Name="TaskTypeLabel" Margin="12,39,0,0" …Run Code Online (Sandbox Code Playgroud) 我试图设置我的菜单项的图标 -
<Grid>
<Grid.Resources>
<Image
x:Key="ReportIconImage" Height="20" Width="20"
Source="/Resource/flag.png"/>
<Image
x:Key="ReportIconImage1" Height="20" Width="20"
Source="/Resource/flag.png"/>
</Grid.Resources>
<Menu Height="22" Margin="0,9,0,0" Name="menu1" VerticalAlignment="Top">
<MenuItem Header="Menu">
<MenuItem Header="Save" ></MenuItem>
<MenuItem Header="Open"/>
<MenuItem Header="Exit"/>
<MenuItem.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}">
<Setter
Property="Icon"
Value="{StaticResource ReportIconImage}">
</Setter>
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
<MenuItem Header="Edit">
<MenuItem Header="Undo"/>
<MenuItem Header="Redo"/>
<Separator/>
<MenuItem Header="Cut"/>
<MenuItem Header="Copy"/>
<MenuItem Header="Paste"/>
<MenuItem.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}">
<Setter
Property="Icon"
Value="{StaticResource ReportIconImage1}">
</Setter>
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
</Menu>
</Grid>
Run Code Online (Sandbox Code Playgroud)
但仅显示最后一个菜单项的图标,而不是前两个.

示例应用程序 - http://weblogs.asp.net/blogs/akjoshi/Samples/WPFMenuItemBugSample.zip
任何人都可以提供此行为的原因和可能的解决方案/解决方法.
我有一个ListBox声明,就像我在那里展示.关键是在Item定义中我有很多关于网格和元素的东西.我想仅更改项目中一个图像的可见性,使其仅在选择元素本身时才可见.
例如,我设法更改了项目的背景和一般外观,但是我无法访问内部元素:(
<ListBox stuff stuff stuff>
<ListBox.ItemTemplate>
<DataTemplate DataType="local:Patient">
<grids , borders, things, stuff
<Image Name="image1" source opacity stuff/>
</ grids bordes and design in general>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="White"/>
<!--HERE I WANT TO CHANGE VISIBILITY OF THE IMAGE-->
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.Template>
<!-- some other styles when deselected and other things -->
</ListBox.Template>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
我试过用:
<Setter TargetName="physiciansSettinsImage" Property="Visibility" Value="Visible"/>
Run Code Online (Sandbox Code Playgroud)
但它不能在Style Setter上设置.任何线索?
整个设计相当复杂,所以我想尽量避免重新编码.
我尝试使用 TreeView 显示分层数据,并且我想为不同的子类型设置不同的数据模板。
但问题是,我的风格没有得到应用。
也许这是一个非常简单的错误,但我真的没有找到它。
<TreeView ItemsSource="{Binding List}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Main}" ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Property1}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:Type2}">
<HierarchicalDataTemplate.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="True"/>
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
<TextBlock Text="{Binding Property2}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:Type3}">
<HierarchicalDataTemplate.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="False"/>
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
Run Code Online (Sandbox Code Playgroud) wpf treeview xaml hierarchicaldatatemplate itemcontainerstyle
我有一个WPF应用程序,我在其中设置了几个组件,看起来更像是默认的Windows 7风格.现在我想对ListView做同样的事情,因为默认样式(即使在Windows 7上)看起来不太相似.
ListView将其View设置为带有一些GridViewColumns的GridView.没有应用任何样式的ListView的XAML是这样的:
<ListView ItemsSource="{Binding Series.ServiceSeries.Weeks}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="" DisplayMemberBinding="{Binding WeekNumber}" />
<GridViewColumn Header="Start date" DisplayMemberBinding="{Binding Path=., Converter={StaticResource WeekConverter}, ConverterParameter=start}" />
<GridViewColumn Header="Track" DisplayMemberBinding="{Binding Path=., Converter={StaticResource WeekConverter}, ConverterParameter=track}" />
<GridViewColumn Header="Race length" DisplayMemberBinding="{Binding Path=., Converter={StaticResource WeekConverter}, ConverterParameter=length}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
Run Code Online (Sandbox Code Playgroud)
在Windows 7上看起来像这样,因为你可以看到颜色略有错误: 1 http://www.nickthissen.nl/Images/Persistent/tmp3DCB.png
所以我决定将自定义样式应用于ListView的ItemContainerStyle.Style是我用于列表框的样式的几乎完全副本,除了用ListViewItem替换ListBoxItem:
<Style TargetType="{x:Type ListViewItem}" x:Key="ListViewItemStyle">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border x:Name="Border" CornerRadius="3" BorderThickness="1" SnapsToDevicePixels="True">
<Border x:Name="InnerBorder" CornerRadius="2" BorderBrush="Transparent" Background="Transparent" …Run Code Online (Sandbox Code Playgroud) 我有代码:
<ListBox Style="{StaticResource DeviceListBox}"
ItemsSource="{Binding MeterList, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
SelectedItem="{Binding CurrentMeter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
ItemContainerStyleSelector="{StaticResource DeviceListItemStyleSelector}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Style="{StaticResource DeviceListText}" Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)
我ItemContainerStyleSelector="{StaticResource DeviceListItemStyleSelector}"用来改变每个列表框项目中的背景颜色(例如黑色或银色,请参阅 - http://msdn.microsoft.com/en-us/library/system.windows.controls.styleselector.aspx).它有效.但是,如果我添加ItemContainerStyle="{StaticResource DeviceListItemStyle}"创建一些触发器等DeviceListItemStyle然后DeviceListItemStyleSelector不起作用.请帮帮我!)
我将A绑定ToolBar到命令视图模型对象的集合。集合中的对象具有一个属性IsSeparator,当为true时,我想<Separator/>在中用表示ToolBar。
我的基本标记如下所示:
<ToolBar Grid.Row="1" ItemsSource="{Binding Path=ToolBarCommands}">
<ToolBar.ItemTemplate>
<DataTemplate>
<Button ToolTip="{Binding Path=ToolTip}" Command="{Binding Path=Command}">
<Button.Content>
<Image Width="16" Height="16" Source="{Binding Path=IconStream}"/>
</Button.Content>
</Button>
</DataTemplate>
</ToolBar.ItemTemplate>
</ToolBar>
Run Code Online (Sandbox Code Playgroud)
我在s的这个示例中玩过ItemContainerStyle很多,但没有用。MenuItem
任何帮助表示赞赏。
wpf ×8
xaml ×3
c# ×2
itemscontrol ×2
listbox ×2
.net ×1
binding ×1
datatemplate ×1
gridview ×1
itemsource ×1
itemtemplate ×1
listboxitem ×1
listview ×1
menuitem ×1
separator ×1
setter ×1
styles ×1
toolbar ×1
treeview ×1
winrt-xaml ×1