XamDataGrid (2011 - 2012): How do I replace a fiel

2019-08-07 16:17发布

I've got a XamDataGrid containing a few fields including these two:

<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>

Due to changing requirements and a bit of time pressure I need to find a way to dynamically change the fields' converter and EditorStyle depending on some value (boolean flag most likely) being present. I've seen general examples using style selectors, but not how to (if even possible) apply a style selector to elements of a XamDataGrid. My initial thought was to use a style selctor class and add two styles for each case, i.e. one for when the field should use the DivideBy1000Converter + DecimalMWhStyle combo and one for when the field should use an empty converter and a PercentageStyle, but along the way I got stuck and I right now I can't figure out how to best achieve what I want.

Does anyone have a good solution to this?

1条回答
时光不老,我们不散
2楼-- · 2019-08-07 16:53

use behaviors to do this quickly.

just build all the styles and field laouts in XAML code and use them inside a behaviour. just look at below code :

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;

I am generating fieldlayout at run time for my grid. And also using the "DataRecordCellAreaStyle", "CellValuePresenterStyle", "LabelPresenterStyle" at run time. This gives you flexibility to permute different styles/converters very easily and it's quite easy to implement and change.

查看更多
登录 后发表回答