- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 DataTemplate 下使用 ItemsControl。我想使用 id 列对 ItemsControl ic 进行排序。
<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.Tried CollectionViewSource 在 xaml 中的 ControlTemplate 下也不起作用。
<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>
但我初始化了 ic 的 Loaded 事件并尝试从那里进行排序。在这种情况下,最初在页面加载时,项目未排序。但是当我移动到另一个用户控件并返回到这个当前用户控件时,项目看起来很完美。
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 - 在 View 模型中对源集合 (seg) 进行排序。
2 - 使用 CollectionViewSource (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 和一个 DataTemplate 来描述 VM 的显示方式:
<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 将显示已排序的项目。
关于DataTemplate下的WPF排序ItemsControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23006286/
我正在使用 DataTemplate 将 View 应用于 ViewModel。我有一种情况,当 View 的一个实例(DataTemplate)中发生某件事时,我需要在所有其他实例中采取行动。 我已
如何从 DataTemplate 派生新类,然后用它代替 DataTemplate? 例如,在 C# 中: public class DerivedDataTemplate : DataTemplat
我有以下xaml; 当我使用 Node 模板显示 No
我想在 dataTemplate 中使用 dataTemplale。我想像这样在列表框中显示数据: 这就是我得到的。它不起作用。 cl
大家好,我正在尝试更改 dataGrid 中列的模板,但我找不到在 XAML 中执行此操作的方法。我正在尝试通过这种方式来做
wpf 应用程序中的默认 DataTemplate 显示 .ToString() 方法的结果。我正在开发一个默认 DataTemplate 不显示任何内容的应用程序。 我试过:
我发现自己有点束手无策……有束缚……呵呵……(跛脚) 无论如何...我需要引用主 ViewModel 的属性,但来自 DataTemplate ,它本身在另一个 DataTemplate 中... 看
我已经为我喜欢的项目中的一些数据类型创建了几个数据模板。这些数据模板真的很酷,因为它们像魔术一样工作,无论何时何地出现在 UI 中,都神奇地改变了数据类型实例的外观。现在我希望能够在一个特定的列表框中
这个问题在这里已经有了答案: 11年前关闭。 Possible Duplicate: Datatemplate inheritance 我有几个不是子类的数据类型,它们也不共享一个接口(interfa
我有一个用代码隐藏在 XAML 中定义的 ResouceDictionary。我需要使用鼠标事件和数据绑定(bind)定义一些特定于 View 的行为,为此我需要访问在 DataTemplate 中定
我有一个用代码隐藏在 XAML 中定义的 ResouceDictionary。我需要使用鼠标事件和数据绑定(bind)定义一些特定于 View 的行为,为此我需要访问在 DataTemplate 中定
我创建了一个 DataTemplateSelector,它使用一组已知接口(interface)进行初始化。如果传递到选择器的项目实现了这些接口(interface)之一,则返回关联的数据模板。 首先
是否可以将数据模板用于没有 ListBox 或其他项目控件的单个项目? 我有一个数据模板,我想在 xaml 中实例化,而不是在列表中,只是在边框内,并设置其数据上下文。 类似(伪):
在我的 MainWindow.xaml 中,我对 ResourceDictionary 有以下引用: 在 Main
我在数据模板中有 WPF ComboBox(列表框中有很多组合框),我想处理输入按钮。如果它是例如,那将很容易。一个按钮 - 我会使用命令 + 相对绑定(bind)路径等。不幸的是,我不知道如何使用命
我想对 ItemsCollection 进行数据绑定(bind),但不是渲染集合项,而是渲染通过集合项上的属性到达的子对象。 更具体地说:这将是游戏的 2D map 查看器(尽管在当前状态下它还不是
我使用 MasterDetailsView控制。它有 MasterHeaderTemplate属性(property)。我想添加一个 TextBox以便执行元素搜索。我不明白该怎么做。因为DataTe
我为自己设计的一个非常基本的 WPF 练习遇到了一个奇怪的问题,即从 ViewModel 动态填充菜单。给定以下主窗口标记:
在设计新的 WPF 应用程序时,我注意到在使用 DataTemplates 对控件进行数据绑定(bind)期间不会抛出异常。为了测试,我用尽可能少的逻辑编写了以下简单的用户控件。我正在使用在 WIN7
我有一个关于 this one 的问题:我正在尝试将一个事件附加到我的 StackPanel,但在使用 XamlReader 时它似乎没有连接。我无法调用 ChildItem_Load 方法。有谁知道
我是一名优秀的程序员,十分优秀!