找不到这个答案.
我有一个WPF ListView控件,可以包含不同数量的列.例如,它可以显示客户数据,显示列ID,名称,电子邮件等,或者它可以包含产品,显示ID,名称,价格,NumberInStock,制造商,以及你得到的想法:不同数量的列,不同的名称.
我想要做的是,某些列以不同的方式显示数据.例如,我不想打印"是"或"否"作为NumberInStock列的值,而是想显示一个整洁的图像.
如果我有固定数量的列,并且要绑定固定名称,我会看到这很简单.只需为该特定列定义DataTemplate,我就可以使用它来定义列的视图.但是,在我的情况下,我无法看到如何做到这一点.
我是WPF的新手,请原谅我,如果我的方法不好:-)在我的XAML中,我已经定义了一个ListView控件,它几乎是空的.在我的代码背后,我使用:
// get all columns from my objects (which can be either a Customer of Product)
foreach (string columnName in MyObject.Columns)
{
GridViewColumn column = new GridViewColumn();
// Bind to a property of my object
column.DisplayMemberBinding = new Binding("MyObject." + columnName);
column.Header = columnName;
column.Width = 50;
// If the columnname is number of stock, set the template to a specific datatemplate defined in XAML
if (columnName == "NumberInStock")
column.CellTemplate = (DataTemplate)FindResource("numberInStockImageTemplate");
explorerGrid.Columns.Add(column);
}
Run Code Online (Sandbox Code Playgroud)
好吧,我相信这可以做得更漂亮一点(如果你有任何建议,请!)但最大的问题是我看不到专栏中的任何差异.它只显示"NumberInStock"列的文本值.我的DataTemplate在XAML中定义: …
我有一个DataTemplate我CellTemplate用作a的GridViewColumn.
我想写这样的东西DataTemplate:
<DataTemplate
x:Key="_myTemplate">
<TextBlock
Text="{Binding Path={Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GridViewColumn}}, Path=Header}}" />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
我GridView被绑定到DataTable了,我要绑定的列的DataTable名字等于的头GridViewColumn的DataTemplate连接到.[我希望这是有道理的!]
不幸的是,这不起作用.我得到一个XamlParseException:"无法在'Binding'类型的'Path'属性上设置''绑定'.'绑定'只能在DependenceyObject的DependencyProperty上设置."
我怎么设置它?
编辑(由DanM提升评论的问题)
我基本上需要一个DataTemplate绑定由连接的DataContext列和哪个列决定DataTemplate.还有其他选择吗?
我有一个自定义控件,它有一个ContentPresenter,它将内容设置为任意对象.此对象对其类型没有任何约束,因此我希望此控件基于应用程序或Generic.xaml中定义的数据模板定义的任何数据模板显示其内容.如果在应用程序中我定义了一些数据模板(没有键,因为我希望它自动应用于该类型的对象),并且我使用绑定到该类型对象的自定义控件,数据模板将自动应用.但我在generic.xaml中为某些类型定义了一些数据模板,我在其中定义了自定义控件样式,并且这些模板未自动应用.这是generic.xaml:
<DataTemplate DataType="{x:Type PredefinedType>
<!-- template definition -->
<DataTemplate>
<Style TargetType="{x:Type CustomControl}">
<!-- control style -->
</Style>
Run Code Online (Sandbox Code Playgroud)
如果我将"PredefinedType"类型的对象设置为contentpresenter中的内容,则不会应用数据模板.但是,如果我在app.xaml中为使用自定义控件的应用程序定义数据模板,则它可以正常工作.
有人有线索吗?我真的不能假设控件的用户将定义这个数据模板,所以我需要一些方法来将它与自定义控件联系起来.
即使在SO上已经存在一些相关问题,我也无法找到解决以下问题的干净解决方案.
如果我有一个多次使用的数据模板,例如TreeViewItem.HeaderTemplate,我怎样才能更改某些TreeViewItems的模板.
例如,假设我的TVI HeaderTemplate有一个文本块,根据字符串,我想使字体粗体.
我想做这样的事情:
((TextBlock)myTreeView.Items.ElementAt(0).FindName("myTextBlock")).FontWeight = FontWeights.Bold;
Run Code Online (Sandbox Code Playgroud)
有人有解决方案吗? - >谢谢埃文
编辑:有没有办法编写泛型函数来获取基于它的名称的控件,即使它在数据模板中?
LayoutRoot.FindName("myTextBlock");如果myTextBlock不在datatemplate中,它将起作用.我怎么写findElementInDataTemplate(string elementName, string parentName)函数?
Evan的回答不是我想要的原因是因为我正在开发一个控件.我希望使用我的控件的应用程序开发人员能够更改控件中的任何元素.如果我使用Evan的解决方案,则需要应用程序开发人员访问控件中的所有模板.可能,但不理想.谢谢!
我的Treelistview控件中有两个DataTemplates用于不同的单元格.模板有90%的相似性.如何重用相同的Xaml?
<DataTemplate x:Key="@names">
<TextBlock Name="txt" Text="{Binding Names}" Style="{DynamicResource @BasicTextBlockStyle}"
Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Border},AncestorLevel=1}, Path=BorderBrush,Mode=OneWay}" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsVisible}" Value="True">
<Setter Property="Style" Value="{DynamicResource @HeaderTextBlockStyle}" TargetName="txt" />
</DataTrigger>
<DataTrigger Binding="{Binding IsScoped}" Value="True">
<Setter Property="Style" Value="{DynamicResource @RootElementTextBlockStyle}" TargetName="txt" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
<DataTemplate x:Key="@Ages">
<TextBlock Name="txt" Text="{Binding Age}" Style="{DynamicResource @BasicTextBlockStyle}"
Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Border},AncestorLevel=1}, Path=BorderBrush,Mode=OneWay}" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsVisible}" Value="True">
<Setter Property="Style" Value="{DynamicResource @HeaderTextBlockStyle}" TargetName="txt" />
</DataTrigger>
<DataTrigger Binding="{Binding IsScoped}" Value="True">
<Setter Property="Style" Value="{DynamicResource @RootElementTextBlockStyle}" TargetName="txt" />
</DataTrigger>
</DataTemplate.Triggers> …Run Code Online (Sandbox Code Playgroud) 是否可以在数据模板中显式使用CollectionViewSource?通常我们将CollectionViewSource放在模板旁边的资源中,但我们的模型不允许这样做,因为collectionviewsource的'source'是树中此级别的DataContext的属性,这意味着需要有一个实例在这个水平.把它放在资源的根部就意味着只有一个实例.我们也不能简单地在外层使用分组,因为这些项目不存在,直到您在层次结构中这么远,并且并非所有兄弟都拥有此属性.因此,逻辑上我们在DataTemplate中实例化CollectionViewSource(在这个例子中是HierarchicalDataTemplate,但这是无关紧要的.)
具体来说,我们尝试在此特定节点级别允许特定排序.我们唯一的另一种选择是在ViewModel本身进行排序,但由于我们使用的ObservableCollections本身不支持排序,因此会变得很痛苦.实际上,我们在这个主题上看到的每篇文章都说明你应该正是因为这个原因使用CollectionViewSource,因此这个问题.
例如,这有效......
<HierarchicalDataTemplate x:Key="CategoryTemplate"
ItemTemplate="{StaticResource TreeViewSymbolTemplate}"
ItemsSource="{Binding Symbols}">
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
</HierarchicalDataTemplate>
Run Code Online (Sandbox Code Playgroud)
但这不......
<HierarchicalDataTemplate x:Key="CategoryTemplate"
ItemTemplate="{StaticResource TreeViewSymbolTemplate}">
<HierarchicalDataTemplate.ItemsSource>
<Binding>
<Binding.Source>
<CollectionViewSource Source="{Binding Symbols}" />
</Binding.Source>
</Binding>
</HierarchicalDataTemplate.ItemsSource>
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
</HierarchicalDataTemplate>
Run Code Online (Sandbox Code Playgroud)
在我看来它会像它一样,但事实并非如此.同样,我们不能将CollectionViewSource放在与数据模板相同的级别,因为每个模板需要一个实例,因为每个模板都有自己的一组项目(尽管它们都将共享排序条件.)
中号
data-binding wpf datatemplate observablecollection collectionviewsource
嘿所有,我正在尝试将动画应用到任何添加到WP7的silverlight子集中的datatemplate的元素.我有一些问题.虽然在使用DataTemplate.Triggers的WPF(Animate WPF Datatemplate,当项目添加到Listbox?)时似乎很有可能,但WP7的silverlight没有DataTemplates的Triggers属性.因此,我将如何在winphone上执行此操作?
silverlight listbox datatemplate windows-mobile windows-phone-7
我有一个XML文件(见下文),可以在列表框中显示所有产品名称.我希望列表框中的每个条目都显示产品名称,后跟价格,而不仅仅是产品名称.
如何在XAML文件中执行datatemplate?谢谢.
简化的XML文件:
<Product>
<Name>Red Chair</Name>
<Price>29.5</Price>
</Product>
Run Code Online (Sandbox Code Playgroud)
简化的XAML文件:
<DockPanel>
<ListBox Name="listBox1" ItemsSource="{Binding}" Margin="10" >
</ListBox>
</DockPanel>
Run Code Online (Sandbox Code Playgroud)
在我的C#文件中,我使用LINQ从XML文件中收集产品并将var产品分配给listBox1.DataContext,它工作正常.现在我只想添加价格.谢谢.
有什么区别
我正在尝试在Xamarin Forms中创建一个自定义视图,转换为IOS中的UICollectionView.
这第一件事很简单:
视图:
public class CollectionView : View
{
}
Run Code Online (Sandbox Code Playgroud)
渲染:
public class CollectionViewRenderer : ViewRenderer<CollectionView, UICollectionView>
{
protected override void OnElementChanged(ElementChangedEventArgs<CollectionView> e)
{
base.OnElementChanged(e);
if (Control == null)
{
SetNativeControl(new UICollectionView(new CGRect(0, 0, 200, 200), new UICollectionViewFlowLayout()));
}
if (e.NewElement != null)
{
...
Control.Source = new CollectionViewSource(a, this);
Control.ReloadData();
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想用DataTemplates(来自DataTemplateSelector)提供这个CollectionView.但我无法找到注册类的方法:
从模板中你可以做到:
Template.CreateContent();
Run Code Online (Sandbox Code Playgroud)
获取UI元素.
但是如何在collectionView中将它注册到CollectionSource中以便出列
例如:
CollectionView.RegisterClassForCell(typeof(????), "CellId");
Run Code Online (Sandbox Code Playgroud) datatemplate xamarin.ios datatemplateselector uicollectionview xamarin.forms
datatemplate ×10
wpf ×7
c# ×3
xaml ×3
silverlight ×2
binding ×1
data-binding ×1
itemtemplate ×1
linq ×1
listbox ×1
listview ×1
resources ×1
styles ×1
xamarin.ios ×1
xml ×1