如何创建可嵌套对象
一开始我想打个招呼,所以……你好。我需要创建一个嵌套的对象结构。 我有一个像这样的对象(xaml 页面)
<navigation:Page x:Class="ItemTemplateExample.ContentItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="200" d:DesignHeight="20"
Title="ContentItem Page">
<Grid x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="button1" />
<ContentControl Grid.Column="1" x:Name="content1" />
</Grid>
现在我想在嵌套结构中使用它。例如
<local:ContentItem ButtonText="level 1">
<local:ContentItem ButtonText="level 2" />
<local:ContentItem ButtonText="level 2" />
<local:ContentItem ButtonText="level 2">
<local:ContentItem ButtonText="level 3" />
</local:ContentItem>
</local:ContentItem>
,其中 ButtonText 将是已知值(添加嵌套对象时设置),每个对象的内容将是相同类型的对象。我不知道如何开始。请给我一些提示,例如。谢谢。
At the begining I would like to say hello, so ... Hello. I need to create a nested object structure.
I've got an object (xaml page) like this
<navigation:Page x:Class="ItemTemplateExample.ContentItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="200" d:DesignHeight="20"
Title="ContentItem Page">
<Grid x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="button1" />
<ContentControl Grid.Column="1" x:Name="content1" />
</Grid>
Now I would like to use it in nested structure. For Example
<local:ContentItem ButtonText="level 1">
<local:ContentItem ButtonText="level 2" />
<local:ContentItem ButtonText="level 2" />
<local:ContentItem ButtonText="level 2">
<local:ContentItem ButtonText="level 3" />
</local:ContentItem>
</local:ContentItem>
Where ButtonText will be known value (set when adding nested object) and Content of each object will be object of the same type. I have no idea how to start. Please give me some hint, exmaples. Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您希望在 WPF 中查看 HierarchicalDataTemplate
参见此处
you would like to see HierarchicalDataTemplate in WPF
Seee here
两种可能的解决方案:用户控件或数据模板。用户控件可能是从您现在所处的位置最简单的方法,但从长远来看,数据模板可能是更好的解决方案。
将用户控件添加到项目中非常简单 - Visual Studio 有一个用于此目的的模板。您可以将此处显示的完全相同的
Grid
放入ContentItem
用户控件的 Xaml 中。然后您只需要在用户控件的代码隐藏中定义一个名为ButtonText
的属性。这应该可以做到:这应该让您或多或少像您所展示的那样使用 Xaml。
我建议数据模板可能更好的原因是您可能不希望在 Xaml 中对嵌套结构进行硬编码。能够从数据生成这样的结构而不是将其烘焙到 UI 中通常是件好事。但是,如果您的结构完全固定,那么您要求的方法可能就可以了。
Two possible solutions: a user control or a data template. The user control is probably the simplest to get to from where you are now, but the data template may be a better solution in the long run.
Adding a user control to a project is straightforward - Visual Studio has a template for that. You could just put the exact same
Grid
you've shown here inside yourContentItem
user control's Xaml. And then you'd just need to define a property calledButtonText
in the codebehind of your user control. This should do it:That should let you use Xaml more or less exactly like you've shown.
The reason I'm suggesting that a data template might be better is that you might not want to hard-code the nested structure in Xaml. It's often nice to be able to generate such structure from data rather than baking it into the UI. However, if your structure is completely fixed, the approach you've asked for is probably OK.
可能相关:嵌套示例收藏
Possibly related:Example of Nested Collections