我有一个XamDataGrid包含几个字段,包括这两个:
<igDP:Field Name="MinValue" Label="Min." Converter="{StaticResource DivideBy1000Converter}" Column="5">
<igDP:Field.Settings>
<igDP:FieldSettings CellWidth="60"
AllowEdit="True"
CellValuePresenterStyle="{StaticResource minValueCellEnabled}"
EditAsType="{x:Type System:String}"
EditorStyle="{StaticResource DecimalMWhStyle}"
CellClickAction="EnterEditModeIfAllowed" />
</igDP:Field.Settings>
</igDP:Field>
<igDP:Field Name="MaxValue" Label="Max." Converter="{StaticResource DivideBy1000Converter}" Column="6">
<igDP:Field.Settings>
<igDP:FieldSettings CellWidth="60"
AllowEdit="True"
CellValuePresenterStyle="{StaticResource maxValueCellEnabled}"
EditAsType="{x:Type System:String}"
EditorStyle="{StaticResource DecimalMWhStyle}"
CellClickAction="EnterEditModeIfAllowed" />
</igDP:Field.Settings>
</igDP:Field>
由于需求的变化和时间压力,我需要找到一种方法来动态更改字段的转换器和EditorStyle,具体取决于某些值(最有可能是布尔标志)。我已经看过使用样式选择器的一般示例,但不是如何(如果可能的话)将样式选择器应用于XamDataGrid的元素。我最初的想法是使用样式selctor类并为每个案例添加两个样式,即一个用于字段应该使用DivideBy1000Converter + DecimalMWhStyle组合,一个用于字段应该使用空转换器和PercentageStyle,但一路上我卡住了,我现在无法弄清楚如何最好地实现我想要的东西。
有没有人有这个好的解决方案?
答案 0 :(得分:0)
使用行为快速完成此操作。
只需在XAML代码中构建所有样式和字段,并在行为中使用它们。看看下面的代码:
FieldLayout sourceFieldLayout = (Infragistics.Windows.Utilities.GetAncestorFromType(this.AssociatedObject, typeof(LocationMatchingView), false) as LocationMatchingView).Resources["LocationMatchingSourceFieldlayout"] as FieldLayout;
foreach (Field field in sourceFieldLayout.Fields)
{
Field newField = new Field(field.Name, field.Label);
newField.Tag = field.Tag;
newField.Width = field.Width;
newField.Settings.CellValuePresenterStyle = field.Settings.CellValuePresenterStyle;
fieldLayout.Fields.Add(newField);
}
fieldLayout.Settings.DataRecordCellAreaStyle =
(Infragistics.Windows.Utilities.GetAncestorFromType(this.AssociatedObject,
typeof(LocationMatchingView), false) as LocationMatchingView).Resources["CAMDataRecordCellAreaStyle"] as Style;
fieldLayout.Settings.AutoGenerateFields = false;
fieldLayout.Settings.FilterUIType = FilterUIType.LabelIcons;
fieldLayout.Settings.AllowFieldMoving = AllowFieldMoving.WithinLogicalRow;
fieldLayout.FieldSettings.CellValuePresenterStyle =
(Infragistics.Windows.Utilities.GetAncestorFromType(this.AssociatedObject,
typeof(LocationMatchingView), false) as LocationMatchingView).Resources["CAMCellValuePresenterStyle"] as Style;
fieldLayout.FieldSettings.LabelTextAlignment = System.Windows.TextAlignment.Center;
fieldLayout.FieldSettings.LabelTextWrapping = TextWrapping.Wrap;
fieldLayout.FieldSettings.LabelTextAlignment = TextAlignment.Justify;
fieldLayout.FieldSettings.LabelClickAction = LabelClickAction.SortByOneFieldOnly;
fieldLayout.FieldSettings.AllowRecordFiltering = true;
fieldLayout.FieldSettings.FilterLabelIconDropDownType = FilterLabelIconDropDownType.MultiSelectExcelStyle;
fieldLayout.FieldSettings.AllowEdit = false;
fieldLayout.FieldSettings.LabelPresenterStyle =
(Infragistics.Windows.Utilities.GetAncestorFromType(this.AssociatedObject,
typeof(LocationMatchingView), false)
as LocationMatchingView).Resources["LocationMatchingLabelPresenterStyle"] as Style;
fieldLayout.FieldSettings.AllowFixing = AllowFieldFixing.NearOrFar;
fieldLayout.Settings.FixedFieldUIType = FixedFieldUIType.Splitter;
fieldLayout.Settings.AllowClipboardOperations = AllowClipboardOperations.Copy;
我在运行时为我的网格生成fieldlayout。并且在运行时使用“ DataRecordCellAreaStyle ”,“ CellValuePresenterStyle ”,“ LabelPresenterStyle ”。这使您可以非常轻松地灵活地置换不同的样式/转换器,并且很容易实现和更改。