WPF DataTemplate 下的排序 ItemsControl

2024-01-21

我在 DataTemplate 下使用 ItemsControl。我想对 ItemsControl 进行排序ic使用 id 列。

   <DataTemplate x:Key="With">
        <DockPanel>               
            <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
                <TextBlock Text="{Binding Path=fil}" Style="{StaticResource Fixed}" Margin="0,0,6,8" />
                <mui:ModernButton  IconData="{StaticResource PlayIconData}" Click="FullPlayback" Margin="0,0,6,8" ></mui:ModernButton>
            </StackPanel>
            <StackPanel DockPanel.Dock="Left" Orientation="Horizontal">
                <TextBlock Text="{Binding Path=e1}" Foreground="Red" Style="{StaticResource Fixed}" Margin="0,0,6,8" />
                <TextBlock Text="{Binding Path=m1}" Foreground="LightSalmon" Style="{StaticResource Fixed}" Margin="0,0,6,8" />
                <TextBlock Text="{Binding Path=n1}" Foreground="Orange" Style="{StaticResource Fixed}" Margin="0,0,6,8" />
                <TextBlock Text="{Binding Path=m2}" Foreground="LightGreen" Style="{StaticResource Fixed}" Margin="0,0,6,8" />
                <TextBlock Text="{Binding Path=m3}" Foreground="Green" Style="{StaticResource Fixed}" Margin="0,0,6,8" />
                <TextBlock Text="{Binding ElementName=H1, Path=Items.Count,Mode=OneWay}" Style="{StaticResource Fixed}" Margin="0,0,6,8" />
            </StackPanel>

            <ItemsControl Name="ic" DockPanel.Dock="Bottom" ItemsSource="{Binding Path=seg}" ItemsPanel="{StaticResource HSPanel}">              
                    <ControlTemplate TargetType="ItemsControl"> 
                        <Border>
                            <ScrollViewer VerticalScrollBarVisibility="Auto">
                                <ItemsPresenter />
                            </ScrollViewer>
                        </Border>
                    </ControlTemplate>
                </ItemsControl.Template>
            </ItemsControl>
        </DockPanel>
    </DataTemplate>

我尝试了以下选项,但排序不起作用。

1.尝试在用户控件的构造函数中进行排序,如下所示(代码隐藏)

ic.Items.SortDescriptions.Clear();
ic.Items.SortDescriptions.Add(new SortDescription("id", ListSortDirection.Ascending));
ic.Items.Refresh();

但我无法访问ic在后面的代码中。错误提示“ic 在当前上下文中不存在”

2.在xaml中的ItemsControl下尝试了CollectionViewSource,但这也不起作用。

<ItemsControl x:Name="ic" DockPanel.Dock="Bottom" ItemsSource="{Binding Path=segments}" ItemsPanel="{StaticResource HSPanel}">
             <ItemsControl.Resources>
                            <CollectionViewSource x:Key="segments"  Source="{Binding seg}">
                                <CollectionViewSource.SortDescriptions>
                                    <scm:SortDescription PropertyName="id" Direction="Ascending"/>
                                </CollectionViewSource.SortDescriptions>
                            </CollectionViewSource>
                        </ItemsControl.Resources>
                <ItemsControl.Template>

3.在xaml中的ControlTemplate下尝试了CollectionViewSource,但这也不起作用。

                    <ControlTemplate TargetType="ItemsControl">                            
                            <ControlTemplate.Resources>
                                <CollectionViewSource x:Key="segments" Source="{Binding seg}" >
                                    <CollectionViewSource.SortDescriptions>
                                        <scm:SortDescription PropertyName="sortId" Direction="Ascending"/>
                                    </CollectionViewSource.SortDescriptions>
                                </CollectionViewSource>
                        </ControlTemplate.Resources>

但我初始化了 Loaded 事件ic并尝试从那里进行排序。在这种情况下,最初加载页面时,项目不会排序。但是,当我移至另一个用户控件并返回当前用户控件时,这些项目看起来完美整理。

    private void ic_Loaded(object sender, RoutedEventArgs e)
    {
        ItemsControl ic = (ItemsControl)sender;
        ic.Items.SortDescriptions.Clear();
        ic.Items.SortDescriptions.Add(new SortDescription("id", ListSortDirection.Ascending));
        ic.Items.Refresh();
    }

您有两个选择:

1 - 在视图模型中对源集合 (seg) 进行排序。

2 - 使用 CollectionViewSource (http://msdn.microsoft.com/fr-fr/library/system.windows.data.collectionviewsource.aspx http://msdn.microsoft.com/fr-fr/library/system.windows.data.collectionviewsource.aspx)。 这是一个完整的工作示例:

我已将此代码添加到空的 WPF 窗口中:

public class SomeVM
{
    public ObservableCollection<SomeItemVM> Items { get; set; }

    public SomeVM()
    {
        Items = new ObservableCollection<SomeItemVM>();
    }
}

public class SomeItemVM
{
    public string id { get; set; }
}

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        //Create some VM
        SomeVM data = new SomeVM();
        data.Items.Add(new SomeItemVM() { id = "3" });
        data.Items.Add(new SomeItemVM() { id = "4" });
        data.Items.Add(new SomeItemVM() { id = "1" });
        data.Items.Add(new SomeItemVM() { id = "2" });

        this.DataContext = data;
    }

}

然后,在 XAML 中,我添加一个用于保存 VM 的内容控件和一个用于描述 VM 显示方式的 DataTemplate:

<Window.Resources>

    <DataTemplate x:Key="With">
        <DockPanel>
            <DockPanel.Resources>
                <!-- CollectionViewSource should be declared as a resource of parent container of the ItemsControl. 
                     Otherwise there will be an exception of StaticResourceHolder --> 
                <CollectionViewSource x:Key="segments" Source="{Binding Items}">
                    <CollectionViewSource.SortDescriptions>
                        <scm:SortDescription PropertyName="id" Direction="Ascending"/>
                    </CollectionViewSource.SortDescriptions>
                </CollectionViewSource>
            </DockPanel.Resources>

            <ItemsControl Name="ic" DockPanel.Dock="Bottom" ItemsSource="{Binding Source={StaticResource segments}}">

                <ItemsControl.Template>
                    <ControlTemplate TargetType="ItemsControl">
                        <Border>
                            <ScrollViewer VerticalScrollBarVisibility="Auto">
                                <ItemsPresenter />
                            </ScrollViewer>
                        </Border>
                    </ControlTemplate>
                </ItemsControl.Template>

                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding id}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DockPanel>
    </DataTemplate>

</Window.Resources>

<Grid>

    <ContentControl Content="{Binding}" ContentTemplate="{StaticResource With}"/>

</Grid>

生成的 ItemsControl 将显示排序的项目。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WPF DataTemplate 下的排序 ItemsControl 的相关文章

  • 如何从页面级别获取父框架?

    我有一个带有框架和几页的窗口 加载窗口时 框架导航到欢迎页面 当我单击欢迎页面内的按钮时 我希望父框架导航到另一个页面 为此 我需要从页面级别访问父框架 但我不知道如何执行此操作 我尝试了下面的代码 但它返回 null private vo
  • WPF 组合框如果只有 1 项则自动选择

    我有一个组合框 我绑定到一个可观察的集合 它会发生变化 根据选择的公司 并且大量公司将有一个帐户 项目 因此我想知道制作组合框的最佳方法是什么如果 ItemsSource 中只有 1 个项目 则设置 SelectedItem 否则将其保留为
  • MultiDataTrigger 使用 OR 而不是 AND

    我正在尝试设置多个DataTriggers on my Button 我做了一些研究发现MultiDataTrigger允许您执行此操作 我想要Visibility我的财产Button如果设置为 falseCCTVPath string E
  • 滚动 X 轴绘图区域 - Silverlight 柱系列

    我有一个工作正常的柱形系列图表 我有一个需要添加的功能 我希望水平滚动能够启用到 x 轴的绘图区域 这是屏幕截图 如果您看到屏幕截图 我有 6 个项目 并且由于项目数量较多 条形图非常细 所以假设如果我有 20 个项目 那么条形图将根本不可
  • 将 WPF 快捷键绑定到 ViewModel 中的命令

    我有一个使用 MVVM 模式的 WPF 应用程序 将按钮连接到 VM 非常简单 因为它们实现了 ICommand 我有一个工作原理类似的上下文菜单 下一步是为上下文菜单创建快捷键 我不知道如何让快捷键调用命令 这是一个例子
  • 重写 WPF 工具包图表中的 DataPointStyle

    我想覆盖DataPointStyle of the LineSeries在我的 WPF 工具包中Chart
  • 列表视图上的 TextBlock:如何忽略 TextBlock 中的点击并让列表视图处理它们

    我有一个显示大量信息的列表视图 但是当它为空时 我想在其上覆盖一个文本块 上面写着 没有要显示的信息 或 bla bla bla 添加信息 列表视图设置为响应鼠标单击 但现在如果我单击文本块 这些事件将路由到文本块 我怎样才能让这些事件转到
  • VisualStateManager 和生成的转换

    正当我以为我明白的时候VisualStateManager 有些事情证明我错了 我正在使用 WPF 4 并且尝试简单地在鼠标悬停时放大某个项目 并在鼠标离开时将其缩小 我想我只需定义每个状态VisualStateGroup然后指定一个Vis
  • Winforms 中的 WPF ElementHost 最大化时崩溃 (Windows)

    我正在尝试将新的 WPF 控件集成到现有的 WinForms 应用程序中 并使用 ElementHost Dock Fill 来托管以下 XAML UserControl NET 4 当我将 WinForm 设置为最大化时 我的整个操作系统
  • ListView ItemContainerStyle 模板

    我创建了用作 ListView 项目模板的样式 其中包含 CheckBox 和 TextBlock
  • WPF 处理文本、图像和文件粘贴事件

    我正在开发一个 WPF 应用程序 我想捕获 RichTextBox 输入中的粘贴命令并处理粘贴的文件 为此 我使用以下回调
  • 混合 MFC 和 WPF:模态对话框

    我使用 C CLI 界面层将 C WPF 对话框添加到现有的 C MFC 应用程序 我一切正常 只是我遇到了形式问题 例如 MFC 应用程序使用 ShowDialog 显示 WPF 对话框 按预期工作 该 WPF 对话框显示使用 DoMod
  • ScrollViewer 滚动条始终禁用

    我是 xaml 和 wpf 的新手 我正在尝试将一些用户控件从代码隐藏插入到容器中 我已阅读此博客文章MSDN http blogs msdn com b marcelolr archive 2009 06 09 stackpanel do
  • 如何在wpf中翻转图像

    我最近学习了如何使用 TransformedBitmap 和 RotateTransformed 类旋转 BitmapImage 现在我可以对图像进行顺时针旋转 但如何翻转图像呢 我找不到执行 BitmapImage 水平和垂直翻转的类 请
  • PreviewKeyDown 不会在 ElementHost 中针对 Tab 和箭头键触发

    我在 Winforms ElementHost 中有一个 WPF 窗口 我的窗口上的 Tab 键和箭头键不会触发 KeyDown 和 PreviewKeyDown 事件 KeyUp 和 PreviewKeyUp 似乎工作正常 Preview
  • 如何枚举控件的所有依赖属性?

    我有一些 WPF 控件 例如 文本框 如何枚举该控件的所有依赖属性 如 XAML 编辑器所做的那样 不需要使用反射 恕我直言 这是一个坏主意 因为框架已经为此提供了实用程序类 但它们并不明显找到 以下是基于这篇文章的答案 枚举绑定 http
  • 如何在 WPF 应用程序上执行异步启动?

    我在异步等待方面相当落后 所以这可能是一个 duh 问题 我正在开发一个非常小的 UI 应用程序 它使用以下命令从系统托盘运行WPF 通知图标 http www codeproject com Articles 36468 WPF Noti
  • 无法将像素着色器渲染到 RenderTargetBitmap!请帮忙!

    我编写了一个非常简单的 WPF 应用程序来测试渲染具有与 RenderTargetBitmap 关联的像素着色器的控件的能力 然后我将位图写入文件 jpeg 该控件被渲染到位图上 但是像素着色器效果不会应用于该控件 代码和 XAML 如下
  • WPF根据数据类型设置样式?

    问题就在这里 我将 TreeView 与几种不同类型的对象绑定 每个对象都是一个节点 有些对象有一个名为 IsNodeExpanded 的属性 当然 有些对象则没有 这是我的风格 现在 问题是当绑定不具有此属性的项目时 我们在输出中收到此错
  • WPF:如何避免图像超出 Canvas 的边界?

    我有一个画布作为图像查看器 它的背景用于放置图像 我想在其上面放置另一个图像 所以 场景是这样的

随机推荐