标签: itemtemplate

在ItemsControl中的每个项目周围包裹一些东西

假设我有一组不同类的对象.每个类在资源文件中都有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?

wpf datatemplate itemtemplate itemscontrol

6
推荐指数
1
解决办法
3732
查看次数

如何使用XAML在WPF中按首字母分组ListBoxItems?

首先,是上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)

wpf xaml itemtemplate listboxitem

6
推荐指数
1
解决办法
8959
查看次数

将嵌套的ListBox绑定到嵌套的List?

我有以下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 listbox itemtemplate

6
推荐指数
1
解决办法
2389
查看次数

WPF - 为 ItemsPresenter 实现 ItemTemplate?

我一直在为 WPF 开发一个 SplitButton 控件,它基本上已经完成,但我正在尝试检查可以在其上设置的所有可能的属性,并确保它们得到实际实现。我主要只剩下两个属性要实现,即ItemTemplateand ItemTemplateSelector(和AlternationCount,好吧,3 个属性)。

我能得到HeaderTemplate并且HeaderTemplateSelector通过绑定工作ContentTemplate,并ContentTemplateSelector给他们上ContentPresenter。这是控件的按钮部分。对于控件的下拉部分,我使用的是 Popup、Border 和ItemsPresenter. 问题是,我想不出如何设置ItemTemplateItemTemplateSelector属性的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)

c# wpf itemtemplate itemspresenter

5
推荐指数
1
解决办法
3万
查看次数

ItemTemplate:ListBox vs ItemsControl

我对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).

wpf listbox itemtemplate itemscontrol

5
推荐指数
1
解决办法
1万
查看次数

WPF以编程方式创建treeview itemtemplate/columns

我有一个应用程序,它读取数据库表并将其放入树视图中.树视图的当前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)

但是,将来可能会有更多列需要添加(由表中不同值的数量决定),所以我试图动态创建它.我该怎么做呢?

c# wpf treeview itemtemplate

5
推荐指数
1
解决办法
2152
查看次数

模板之间的区别

有什么区别

  • 控件模板
  • DataTemplate中
  • HierarchalDataTemplate
  • ItemTemplate中

c# wpf datatemplate itemtemplate controltemplate

5
推荐指数
2
解决办法
2361
查看次数

带有标题和项模板列的Windows 8 XAML ListView应具有相同的动态宽度

我正在使用带有Itemtemplate和Headertemplate的Listview.两个模板都包含6个列.如果我为模板设置固定的列宽,一切都很好 - 如图1所示.
但我想为项目设置宽度为"自动" - 但后来我得到图2 ...

怎么办呢?是否可以使用c#设置标题列宽度? - 或任何其他解决方案?

图1:

http://i.stack.imgur.com/8Ew3g.png

图2:

http://i.stack.imgur.com/mPX4U.png

代码列表视图:

<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)

xaml listview itemtemplate windows-8

5
推荐指数
2
解决办法
2万
查看次数

MvvmCross:MvxListView的动态项模板选择

如果我有一个带有以下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

5
推荐指数
1
解决办法
5137
查看次数

如何从DataTemplate中获取代码背后的元素

我有一个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)

c# wpf datatemplate itemtemplate

5
推荐指数
1
解决办法
3796
查看次数