[英]How can I reuse XAML tags in WPF?
I was wondering whether it was possible to instead of me copying pieces of code in XAML, to define something that I could link to.我想知道是否可以代替我复制 XAML 中的代码片段来定义我可以链接到的内容。 In more concrete terms, what I have is:更具体地说,我所拥有的是:
<Grid>
// Definitions
<Label Content="Name:" />
<Label Content="Age:" Grid.Column="1"/>
...
<Grid>
and now I would like to have something like <Content_in_grid=nice_labels>
and it should simply copy these labels.现在我想要像<Content_in_grid=nice_labels>
这样的东西,它应该简单地复制这些标签。
Notice: I do not want the grid, because these are some labels that I would like to bind with different properties.注意:我不想要网格,因为这些是我想与不同属性绑定的一些标签。
PS: maybe a different perspective, imagine I would like to create a file properties comparison program with WPF. PS:也许换个角度,想象一下我想用 WPF 创建一个文件属性比较程序。 So you can imagine that I have to copy all those labels such as Filename, Date etc. static labels again, but for maintainability, if I would like to change Filename to File then I would have to search and replace instead of editing it in one place.所以你可以想象我必须再次复制所有这些标签,如文件名、日期等静态标签,但为了可维护性,如果我想将文件名更改为文件,那么我将不得不搜索和替换而不是在一个中编辑它地方。
Thanks in advance.提前致谢。
Add it in the resources:在资源中添加:
<Window.Resources>
<DataTemplate x:Key="MyGrid">
<Grid>
<Label Content="Name:" />
<Label Content="Age:" Grid.Column="1" />
</Grid>
</DataTemplate>
</Window.Resources>
And then use it where you want:然后在你想要的地方使用它:
<StackPanel>
<ContentPresenter ContentTemplate="{StaticResource MyGrid}" />
<ContentPresenter ContentTemplate="{StaticResource MyGrid}" />
<ContentPresenter ContentTemplate="{StaticResource MyGrid}" />
</StackPanel>
The answer from Flat Eric is almost correct. Flat Eric 的回答几乎是正确的。
I added x:Shared="False"
though, because otherwise the Resource could only be used once and would only be visible in the last ContentPresenter.不过,我添加了x:Shared="False"
,因为否则资源只能使用一次,并且只能在最后一个 ContentPresenter 中可见。 (It is visible in all ContentPresenters in the VisualStudio, but not in the final application). (它在 VisualStudio 的所有 ContentPresenter 中可见,但在最终应用程序中不可见)。
PS: I saw that Flat Eric edited his answer, so that x:Shared is not necessary anymore. PS:我看到 Flat Eric 编辑了他的答案,所以 x:Shared 不再需要了。 Anyway, the original answer, should be like this:无论如何,原来的答案,应该是这样的:
<Grid x:Key="MyGrid" x:Shared="False" >
<Label Content="Name:"/>
<Label Content="Age:" Grid.Column="1"/>
</Grid>
What you are describing is templating the content based on a collection of items (you mentioned a file properties comparison tool, so I am guessing it could be a list of file properties).您所描述的是基于项目集合对内容进行模板化(您提到了文件属性比较工具,所以我猜它可能是文件属性列表)。
You would in reality not add the Labels
over and over again to a Grid
, but would let the Binding
system do it for you using (in your case) a HeaderedItemsControl
such as a DataGrid
.实际上,您不会将Labels
一遍又一遍地添加到Grid
,而是让Binding
系统使用(在您的情况下)使用HeaderedItemsControl
例如DataGrid
来为您执行此操作。
<DataGrid ItemsSource="{Binding FileProperties}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding PropertyName}" Header="Name:"/>
<DataGridTextColumn Binding="{Binding PropertyValue}" Header="Age:"/>
</DataGrid.Columns>
</DataGrid>
NB : FileProperties
used in the ItemsSource
would normally be an ObservableCollection<T>
of a type that has PropertyName
and PropertyValue
properties.注意:在ItemsSource
使用的FileProperties
通常是一个ObservableCollection<T>
,其类型具有PropertyName
和PropertyValue
属性。
You could achieve a similar effect using an ItemsControl
and some static XAML for the header, but this is much neater overall.您可以使用ItemsControl
和一些用于标头的静态 XAML 来实现类似的效果,但这总体上要简洁得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.