假设我有一组不同类的对象.每个类在资源文件中都有UserControl DataTemplated.
现在我想使用ItemsControl来显示集合,但我希望每个项目周围都有一个Border或Expander.
我希望这样的东西能起作用:
<ItemsControl ItemsSource="{Binding MyObjects}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="3">
<ContentPresenter/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
但是ContentPresenter似乎选择了ItemTemplate,因为我得到了堆栈溢出.
如何在ItemTemplate中获取每个Item的DataTemplate?
首先,这是上ListBox
一篇文章,它处理ObservableCollection<Account>
从AccountsCollection.cs
类中绑定到我的帐户的AccountListBox数据.
所以现在我有一个绑定对象AccountsCollection和一个名为AccountTemplate的DataTemplate,用于我在资源中定义的ListBox:
<Window.Resources>
<controller:AccountsWindowController x:Key="AccountsCollection" />
<DataTemplate x:Key="AccountTemplate">
<DockPanel>
<Button Name="EditButton"
DockPanel.Dock="Right"
Margin="3 0 3 0"
VerticalAlignment="Center"
Content="Edit" />
<Button Name="DeleteButton"
DockPanel.Dock="Right"
Margin="3 0 3 0"
VerticalAlignment="Center"
Content="Delete" />
<TextBlock Name="AccountName"
VerticalAlignment="Center"
Text="{Binding Name}"
TextWrapping="NoWrap"
TextTrimming="CharacterEllipsis" />
</DockPanel>
</DataTemplate>
<Window.Resources>
Run Code Online (Sandbox Code Playgroud)
以下是与LisBox本身相关的代码:
<ListBox Name="AccountsListBox"
Margin="12,38,12,41"
HorizontalContentAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding Accounts,
Source={StaticResource ResourceKey=AccountsCollection}}"
ItemTemplate="{StaticResource ResourceKey=AccountTemplate}"
MouseDoubleClick="AccountsListBox_MouseDoubleClick">
</ListBox>
Run Code Online (Sandbox Code Playgroud)
我希望我的列表能够通过起始字母对所有帐户进行分组,并在列表中显示该字母(我也想将一些设计应用于该字母标题).最终结果应该是这样的:
感谢您的帮助!
更新:这是成功实现分组的代码.
<Window x:Class="Gui.Wpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:entities="clr-namespace:Entities.Accounts;assembly=Entities"
xmlns:contollers="clr-namespace:Gui.Wpf.Controllers"
xmlns:converters="clr-namespace:Gui.Wpf.Converters"
xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
Title="MainWindow"
Width="525"
Height="350" > …
Run Code Online (Sandbox Code Playgroud) 我有以下ViewModel-Property:
public List<List<string>> Names .... //It's a dependency property
Run Code Online (Sandbox Code Playgroud)
在我看来,我希望ItemsControl
有一个ItemsControl
:
<ItemsControl ItemsSource="{Binding Names}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding ?????}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Text="{Binding ??????}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
我如何绑定到的项目List
?在上面的代码示例中,我标记了它?????
我一直在为 WPF 开发一个 SplitButton 控件,它基本上已经完成,但我正在尝试检查可以在其上设置的所有可能的属性,并确保它们得到实际实现。我主要只剩下两个属性要实现,即ItemTemplate
and ItemTemplateSelector
(和AlternationCount
,好吧,3 个属性)。
我能得到HeaderTemplate
并且HeaderTemplateSelector
通过绑定工作ContentTemplate
,并ContentTemplateSelector
给他们上ContentPresenter
。这是控件的按钮部分。对于控件的下拉部分,我使用的是 Popup、Border 和ItemsPresenter
. 问题是,我想不出如何设置ItemTemplate
和ItemTemplateSelector
属性的ItemsPresenter
。
有任何想法吗?
更新:SplitButton 的完整源代码现在可在:http ://anothersplitbutton.codeplex.com/
这是 Luna.NormalColor.xaml 文件:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:WpfSplitButton="clr-namespace:WpfSplitButton"
xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- SplitButtonHeader Style -->
<Style x:Key="{x:Type WpfSplitButton:SplitButtonHeader}"
TargetType="{x:Type WpfSplitButton:SplitButtonHeader}">
<Style.Resources>
<Style x:Key="ButtonFocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="2"
StrokeThickness="1"
Stroke="Black"
StrokeDashArray="1 2"
SnapsToDevicePixels="True" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Style.Resources>
<Style.Setters>
<Setter Property="FocusVisualStyle" …
Run Code Online (Sandbox Code Playgroud) 我对WPF世界很陌生,我在使用ItemsControl模板化项目时遇到了一些问题.我需要的是在ItemsControl(或类似)中模板元素(主要是按钮).
如果我使用以下XAML代码...
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type Button}">
<Border BorderBrush="AliceBlue"
BorderThickness="3">
<TextBlock Text="Templated!"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<Button>Not templated</Button>
<TextBlock>Text not templated</TextBlock>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
...我得到这个结果:http://img444.imageshack.us/img444/2167/itemscontrolnottemplate.gif
该ItemsControl的没有模板套用到按钮也不到TextBlock的控制.如果我将ItemsControl更改为ListBox,如下所示:
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type Button}">
<Border BorderBrush="AliceBlue"
BorderThickness="3">
<TextBlock Text="Templated!"/>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
<Button>Not templated</Button>
<TextBlock>Text not templated</TextBlock>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
...然后我得到了这个结果:img814.imageshack.us/img814/6204/listboxtoomuchtemplatin.gif
现在模板应用于两个子控件(即使我将DataType设置为Button).
我有一个应用程序,它读取数据库表并将其放入树视图中.树视图的当前ItemTemplate如下所示:
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding SubOrganLocations}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="35" />
<ColumnDefinition Width="35" />
<ColumnDefinition Width="35" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding OrganDisplayName}" />
<TextBox Grid.Column="1" IsEnabled="True" />
<TextBox Grid.Column="2" IsEnabled="True" />
<TextBox Grid.Column="3" IsEnabled="True" />
</Grid>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)
但是,将来可能会有更多列需要添加(由表中不同值的数量决定),所以我试图动态创建它.我该怎么做呢?
有什么区别
我正在使用带有Itemtemplate和Headertemplate的Listview.两个模板都包含6个列.如果我为模板设置固定的列宽,一切都很好 - 如图1所示.
但我想为项目设置宽度为"自动" - 但后来我得到图2 ...
怎么办呢?是否可以使用c#设置标题列宽度? - 或任何其他解决方案?
图1:
图2:
代码列表视图:
<ListView x:Name="DayanalyseListView"
HorizontalAlignment="Center"
VerticalAlignment="Top"
ItemTemplate="{StaticResource DataTemplate}"
HeaderTemplate="{StaticResource HeaderTemplate}">
</ListView>
Run Code Online (Sandbox Code Playgroud)
HeaderTemplate中:
<DataTemplate x:Key="HeaderTemplate" >
<Grid Height="36" Background="DarkGray" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="95"/>
<ColumnDefinition Width="85"/>
<ColumnDefinition Width="85"/>
<ColumnDefinition Width="85"/>
<ColumnDefinition Width="85"/>
<ColumnDefinition Width="*" MinWidth="900"/>
</Grid.ColumnDefinitions>
<TextBlock x:Uid="DayProject" TextWrapping="Wrap" Text="Project" Grid.Column="0" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="7,2,0,0" Style="{StaticResource BodyTextStyle}" />
<TextBlock x:Uid="DayTask" TextWrapping="Wrap" Text="Task" Grid.Column="1" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="7,2,0,0" Style="{StaticResource BodyTextStyle}" />
<TextBlock x:Uid="DayFrom" TextWrapping="Wrap" Text="From" Grid.Column="2" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="7,2,0,0" Style="{StaticResource BodyTextStyle}" />
<TextBlock x:Uid="DayTill" …
Run Code Online (Sandbox Code Playgroud) 如果我有一个带有以下MvxListView定义的视图:
<Mvx.MvxListView
android:layout_marginTop="10px"
android:textFilterEnabled="true"
android:choiceMode="singleChoice"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="20dp"
local:MvxBind="ItemsSource Data; ItemClick LaunchCapabilityViewCmd"
local:MvxItemTemplate="@layout/itemtemplate1" />
Run Code Online (Sandbox Code Playgroud)
而不是将MvxItemTemplate硬编码为itemtemplate1,可以根据我想在此视图中显示的数据类型动态设置它吗?我正在寻找与WPF的DateTemplateSelector类似的功能.
TIA.
itemtemplate android-layout mvvmcross xamarin itemtemplateselector
我有一个FlipView控件,其DataTemplate定义如下:
<FlipView x:Name="FlipView5Horizontal" Width="480" Height="270" BorderBrush="Black" BorderThickness="1" Style="{StaticResource FlipViewStyle1}">
<FlipView.ItemTemplate>
<DataTemplate>
<Grid>
<Image Width="480" Name="xxxImage" Height="270" Source="{Binding Image}" Stretch="UniformToFill"/>
<Border Name="xxxBorder" Background="#A5000000" Height="80" VerticalAlignment="Bottom">
<TextBlock Name="xxxTB" Text="{Binding Title}" FontFamily="Segoe UI" FontSize="26.667" Foreground="#CCFFFFFF" Padding="15,20"/>
</Border>
</Grid>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
Run Code Online (Sandbox Code Playgroud)
在后面的代码中,我需要访问名为“ xxxTB”的TextBlock。这是我要做的代码:
public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return …
Run Code Online (Sandbox Code Playgroud) itemtemplate ×10
wpf ×8
c# ×4
datatemplate ×3
itemscontrol ×2
listbox ×2
xaml ×2
listboxitem ×1
listview ×1
mvvmcross ×1
treeview ×1
windows-8 ×1
xamarin ×1