标签: itemscontrol

在DataTemplate中排序绑定的ItemsControl(仅限XAML)

是否有一种XAML方法可以根据项目的某个属性自动对绑定项目(ViewModel对象列表)ItemsControl进行排序.ItemsControl是DataTemplate的一部分.我认为CollectionViewSource可以解决这个问题,但是如何将CollectionViewSource绑定到ItemsControl.以下代码没有任何表现:

<--xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"-->
    <DataTemplate DataType="{x:Type vm:Company}">
        <DataTemplate.Resources>
            <CollectionViewSource x:Key="viewSource" Source="{Binding Employees}">
                <CollectionViewSource.SortDescriptions>
                        <scm:SortDescription PropertyName="ID" />
                    </CollectionViewSource.SortDescriptions>
            </CollectionViewSource>
        </DataTemplate.Resources>
        <Viewbox>
            <ItemsControl ItemsSource="{Binding Source={StaticResource viewSource}}">
                 <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </Viewbox>
    </DataTemplate>
Run Code Online (Sandbox Code Playgroud)

sorting wpf datatemplate itemscontrol collectionviewsource

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

如何在ItemsControl中突出显示所选项?

我有以下XAML.如何突出显示ItemsControl中的所选项?我可以覆盖ListView的选定项目模板,但如何为ItemsControl实现相同的功能?有没有可以显示图像集的替代控件?

<Window x:Class="ImageScrollDemo.View.MoviePosterView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ImageScrollDemo"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    Title="MoviePosterView" Height="367" Width="725" Foreground="White">
<Window.Background>
    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
        <GradientStop Color="#FF505050" Offset="0"/>
        <GradientStop Color="#FF202020" Offset="1"/>
    </LinearGradientBrush>
</Window.Background>
<Window.DataContext>
    <local:MainWindowViewModel />
</Window.DataContext>
<Window.Resources>
    <local:MainWindowViewModel x:Key="ViewModel" />

    <Style TargetType="Image" x:Key="ImageHover">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Opacity" Value="1" />
                <Setter Property="BitmapEffect">
                    <Setter.Value>
                        <OuterGlowBitmapEffect GlowColor="Gold" GlowSize="8"/>
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="False">
                <Setter Property="Opacity" Value="0.7" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <i:InvokeCommandAction Command="{Binding PopulateMovieGrid}" />
    </i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="60" />
    </Grid.RowDefinitions> …
Run Code Online (Sandbox Code Playgroud)

wpf highlight itemscontrol

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

为什么我的ItemsControls中的项目不是水平布局的?

我想要一个ItemsControl,其中项目水平显示.

但是,无论我使用StackPanel的Orientation ="Horizo​​ntal"还是WrapPanel,它们仍然会堆叠起来.

如何让ItemsControl中的项目水平?

alt text http://i31.tinypic.com/dd2et5.png

XAML:

<Window x:Class="TestItemsControl2938.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="400">
    <Window.Resources>
        <DataTemplate x:Key="CustomerListTemplate">
            <StackPanel Width="100" Background="#aaa" Margin="5">
                <TextBlock Text="{Binding LastName}"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>       
    <StackPanel>
        <StackPanel Orientation="Horizontal" Background="Orange">
            <ItemsControl ItemsSource="{Binding CustomerList}" ItemTemplate="{StaticResource CustomerListTemplate}"/>
        </StackPanel>
        <WrapPanel Background="Yellow">
            <ItemsControl ItemsSource="{Binding CustomerList}" ItemTemplate="{StaticResource CustomerListTemplate}"/>
        </WrapPanel>
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

代码隐藏:

using System.Windows;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace TestItemsControl2938
{
    public partial class Window1 : Window, INotifyPropertyChanged
    {
        private ObservableCollection<Customer> _customerList = new ObservableCollection<Customer>();
        public ObservableCollection<Customer> …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml itemscontrol

9
推荐指数
1
解决办法
6692
查看次数

ItemsControl,VirtualizingStackPanel和ScrollViewer高度

我想使用ItemsControl显示重要的项目列表.

我正在使用ItemsControl的原因是DataTemplate在我正在处理的应用程序中要复杂得多:提供的示例代码仅反映了我的调整问题.

我想要 :

  • 要进行虚拟化的ItemsControl,因为要显示许多项目
  • 它的大小自动扩展到其父容器(Grid)

    <Grid>
        <ItemsControl x:Name="My" ItemsSource="{Binding Path=Names}">
            <ItemsControl.Template>
                <ControlTemplate>
                    <StackPanel>
                        <StackPanel>
                            <TextBlock Text="this is a title" FontSize="15" />
                            <TextBlock Text="This is a description" />
                        </StackPanel>
                        <ScrollViewer CanContentScroll="True" Height="400px">
                            <VirtualizingStackPanel IsItemsHost="True" />
                        </ScrollViewer>
                    </StackPanel>
                </ControlTemplate>
            </ItemsControl.Template>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
    
    Run Code Online (Sandbox Code Playgroud)

背后的代码是:

public partial class Page1: Page
{
    public List<string> Names { get; set; }
    public Page1()
    {
        InitializeComponent();

        Names = new List<string>();
        for(int i = 0; i < 10000; i++)
            Names.Add("Name : …
Run Code Online (Sandbox Code Playgroud)

wpf itemscontrol scrollviewer virtualizingstackpanel

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

为什么ItemsControl不使用我的ItemTemplate?

我能够在ItemsControl中使用ItemTemplate以特定格式呈现项目.但是,如果ItemsControl中的一个项恰好是TextBox,则呈现TextBox而不是ItemsTemplate的实例.据我所知,任何FrameworkElement都是如此.这是ItemsControl的预期行为,还是我做错了什么?

一个例子:

<ItemsControl>
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <Grid Margin="5">
        <Rectangle Fill="Blue" Height="20" Width="20" />
      </Grid>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
  <ItemsControl.Items>
    <sys:Object />
    <TextBox />
    <sys:Object />
    <Rectangle Fill="Red" Height="20" Width="20" />
  </ItemsControl.Items>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

我希望这能显示四个蓝色矩形.我认为只要定义了ItemTemplate,集合中的每个项目都会呈现为模板的一个实例.但是,在这种情况下,将呈现以下内容:蓝色矩形后跟TextBox,后跟蓝色矩形,后跟红色矩形.

silverlight wpf itemtemplate itemscontrol

9
推荐指数
1
解决办法
2881
查看次数

ItemsControl中的水平定向WrapPanel垂直列出

我在我的XAML中定义了两个DataTemplates,每个都用于一个单独的ItemsControl面板.

主ItemsControl列出了ObservableCollection对象中存储的Foo对象.

Foo对象本身具有作为ObservableCollection对象存储的自己的一组项.

我试图以一种允许每个ObservableCollection Foo项目在标题中显示的方式定义XAML(第一个ItemsControl).由此,每个Foo项目本身内的列表应该水平显示(使用第二个ItemsControl),下面是相关字段.如果存在足够的物品,那么它们应该在必要时包裹到下一行.

这就是UI目前的表现:

UI不正确

这就是我希望UI实际出现的方式:

正确的用户界面

我的标记(按钮控件用于UI的另一个方面):

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
            <ItemsControl x:Name="ContentList" ItemTemplate="{StaticResource GameTemplate}" Grid.Column="0" />
        </ScrollViewer>
        <StackPanel Grid.Column="1" Background="DarkGray">
            <Button Click="OnLoad">_Load</Button>
            <Button Click="OnSave">_Save</Button>
            <Button Click="OnAdd">_Add</Button>
            <Button Click="OnDelete">_Delete</Button>
        </StackPanel>
    </Grid>
Run Code Online (Sandbox Code Playgroud)

DataTemplate用于列出Foo项目:

<DataTemplate x:Key="GameTemplate">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="30" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Label Content="{Binding Name}" Grid.Row="0" Background="Gray" FontSize="16" />
                <ItemsControl x:Name="imageContent" 
                              ItemsSource="{Binding FileList}" 
                              ItemTemplate="{StaticResource GameImagesTemplate}" 
                              Grid.Row="1" />
            </Grid>
        </DataTemplate>
Run Code Online (Sandbox Code Playgroud)

DataTemplate用于列出每个Foo项目中的项目:

<DataTemplate x:Key="GameImagesTemplate">
            <WrapPanel Orientation="Horizontal">
                <StackPanel Orientation="Vertical" >
                    <Image Source="{Binding FileInfo.FullName}" 
                       Margin="8,8,8,8" 
                       Height="70" …
Run Code Online (Sandbox Code Playgroud)

c# wpf .net-4.0 itemscontrol

9
推荐指数
1
解决办法
9976
查看次数

ListBox分组问题

我正在尝试根据以下模型对我的集合进行分组:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Role PersonRole { get; set; }
}

public class Role
{
    public int Id { get; set; }
    public string RoleName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的PersonCollection,

public ObservableCollection<Person> PersonList;
Run Code Online (Sandbox Code Playgroud)

该集合中填充了三个Person对象,两个具有相同的角色.

我想按照他们的角色名称对所有人进行分组.我有以下XAML:

<Grid.Resources>

        <CollectionViewSource x:Key="cvs" Source="{Binding PersonList}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="PersonRole.RoleName"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>

    </Grid.Resources>

<ListBox ItemsSource="{Binding Source={StaticResource cvs}}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding FirstName}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding …
Run Code Online (Sandbox Code Playgroud)

wpf listbox itemscontrol

9
推荐指数
1
解决办法
5449
查看次数

WPF自定义面板/控件创建 - "甜甜圈"

我希望创建一个自定义面板或控件来创建一个非常特殊类型的项目.

基本上,目的是有一个控件,你可以给出一个对象列表,它会将每个对象放入一个按钮.问题是按钮应该像圆环一样布置成圆形.与下图相似.

甜甜圈图表

但是现在想象一下,如果可以的话,每个彩色部分都是一个按钮.它们也具有按钮的所有功能,如鼠标悬停和事件.

所以,问题首当其冲的是:我应该采用什么样的技术来创造这种控制?有没有办法在按钮上进行某种"曲率"变换?

看起来我真的想在这里找两件事,对吧?

我的意思是,我可以将列表中的每个项目放入一个ItemsControl,它有一个按钮作为其ItemTemplate.所以我需要的是两件事:

第一个是径向布局面板(我见过其中的一些).

第二种方法是让每个按钮都有某种旋转和曲率变换.

有任何想法吗?

wpf panel itemscontrol radial

8
推荐指数
1
解决办法
1461
查看次数

如何为ItemsControl中的项添加边框?

我正在尝试为项目控件中的每个项目设置边框.以下是我的XAML代码.但这不起作用.

<ItemsControl.ItemContainerStyle>
    <Style>
        <Setter Property="Control.BorderThickness" Value="5" />
        <Setter Property="Control.BorderBrush" Value="Black" />
    </Style>
</ItemsControl.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)

wpf xaml styles border itemscontrol

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

WPF:Scroll Itemcontrol内容固定标题

是否可以使用WPF的ItemsControl:Demo执行类似的操作

我试图冻结GroupedItems而不是GridView列.

在此输入图像描述

资源:

<Window.Resources>
    <CollectionViewSource x:Key="data" Source="{Binding}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="Date"/>
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

列表显示:

<ListView Grid.Column="0" ItemsSource="{Binding Source={StaticResource data}}">
    <ListView.View>
        <GridView>
            <GridView.Columns>
                <GridViewColumn Header="Col 1" DisplayMemberBinding="{Binding Col1}" Width="100"/>
                <GridViewColumn Header="Col 2" DisplayMemberBinding="{Binding Col2}" Width="100"/>
                <GridViewColumn Header="Col 3" DisplayMemberBinding="{Binding Col3}" Width="100"/>
            </GridView.Columns>
        </GridView>
    </ListView.View>
    <ListView.GroupStyle>
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>
                                    <Grid Grid.Row="0">
                                        <TextBlock Background="Beige" FontWeight="Bold" Text="{Binding Path=Name, StringFormat={}{0}}"/>
                                    </Grid>
                                    <DockPanel Grid.Row="1">
                                        <ItemsPresenter Grid.Row="2"></ItemsPresenter>
                                    </DockPanel>
                                </Grid>
                            </ControlTemplate> …
Run Code Online (Sandbox Code Playgroud)

wpf gridview itemscontrol scrollview

8
推荐指数
3
解决办法
4269
查看次数