动态特性使您可以显示和编辑指定给 Vault 类别的特性,而无需修改 XAML 文件。
将新特性添加到 Vault 类别时,这些特性将在下次显示 Data Standard 对话框或选项卡时自动显示。
动态特性由包含两个列的 XAML DataGrid 控件表示。第一列为只读,并显示特性名称。第二列中更加复杂,因为它已显示不同的控制不同类型(例如,ComboBox 属性具有预定义的值列表中,DataPicker 属性的类型日期)。
若要将动态特性控件添加到对话框,需要以下行:
...
<DataGrid x:Name="DSDynamicCategoryProperties"
AutoGenerateColumns="False"
HeadersVisibility="Column"
ItemsSource="{Binding DynamicProperties}"
ScrollViewer.CanContentScroll="False"
HorizontalGridLinesBrush="WhiteSmoke"
VerticalGridLinesBrush="WhiteSmoke">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}"
SortDirection="Ascending"
Width="140"
IsReadOnly="True" >
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding DataContext.UIString[LBL64], RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTemplateColumn CellStyle="{StaticResource DynamicDataGridCellStyle}"
Width="*" >
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding DataContext.UIString[LBL65], RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
...
第二列使用样式 (cellstyle="{staticresourcedynamicdatagridcellstyle}),该样式也必须添加到 XAML 文件的“资源”部分。这将处理所有不同的控件:
...
<Window.Resources>
<Style x:Key="DynamicDataGridCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ComboBox ItemsSource="{Binding ListValues}" Text="{WPF:ValidatedBinding Value}" IsEditable="True"></ComboBox>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static DataGrid.FocusBorderBrushKey}}"/>
</Trigger>
<DataTrigger Binding="{Binding ListValues.Count}" Value="0">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<TextBox Text="{WPF:ValidatedBinding Value}"></TextBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Typ}" Value="DateTime">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<DatePicker SelectedDate="{WPF:ValidatedBinding Value, StringFormat='dd/MM/yyyy'}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Typ}" Value="Bool">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ComboBox Text="{WPF:ValidatedBinding Value}">
<system:Boolean>True</system:Boolean>
<system:Boolean>False</system:Boolean>
</ComboBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
...
DynamicProperties在上一个示例中查看以下代码行:
ItemsSource="{Binding DynamicProperties}"
此代码获取指定给类别的 IProperty 元素的列表。除其他成员外,IProperty 对象还具有以下可用于绑定的成员:
ListValues – 用于具有预定义值列表的特性。Name – 特性的名称。该值用于第一列。Value – 用于获取并设置特性值。DynamicProperties 会返回指定给某个类别的所有特性。而对于 CAD,仅返回映射的特性。这是因为适用于 CAD 的 Data Standard 不直接更改 Vault 特性。更改通过 Vault 映射完成。DynamicPropertiesCategory为了使 DynamicProperties 绑定返回内容,必须先指定 Vault 类别。在附带 Data Standard 的默认实施中,通过组合框指定 DynamicPropertiesCategory:
...
<ComboBox
Name="Categories"
ItemsSource="{Binding PsList[GetCategories]}"
SelectedIndex="0"
DisplayMemberPath="Name"
selecteditem="{binding-dynamicpropertiescategory}"
Text="{Binding Prop[_Category].Value}" Grid.Row="0" Grid.Column="1"
IsEnabled="{Binding IsNewEntryDialog}">
</ComboBox>
...
可用 Vault 类别的列表 (ItemsSource="{Binding PsList[GetCategories]}") 由 PowerShell 函数 GetCategories 返回。在默认实现中,这仅会返回所有类别。如果只有某些类别可用(例如,基于文档类型),则必须调整此函数。
从组合框中选择的类别将设置 DynamicPropertiesCategory:
(selecteditem="{binding-dynamicpropertiescategory}")
单击此处可了解有关动态特性标签的详细信息。