我正在构建一个GUI,根据其架构允许的值编辑各种XML配置文件。我需要一种方法来显示左列中每个字段的标签,同时显示一个控件来编辑右列中的该字段。如果这个字段列表不是动态的,我只需在带有两列的Grid中显示它们。但是,对于动态的字段列表,这似乎并不那么容易。
以下示例显示了我想要做的事情,但由于DataTemplate只能包含一个子节点,因此这种方式不起作用。任何人都知道一个好的解决方法,可以正确地进行动态表单布局吗?
(为了简化示例,我使用的是统一网格。我可能更喜欢在ControlTemplate中删除Grid的方法(所以你可以定义列/行) - 但是,我认为这种方法得到了指出)
<ItemsControl x:Name="FieldItemsCtrl">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="2" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type local:EditableField}">
<TextBlock Text="{Binding FieldName}" Foreground="{DynamicResource TextBrush}" />
<TextBox Text="{Binding FieldValue}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
谢谢!
答案 0 :(得分:0)
您可以使用DataTemplate从面板替换所需内容。看看这个:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib" >
<Grid>
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="FieldName" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0, 0, 14, 0" />
<TextBox Text="FieldValue" Grid.Column="1" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
<System:String>Hello</System:String>
<System:String>World</System:String>
</ItemsControl>
</Grid>
</Page>