简体   繁体   中英

XamDataGrid (2011 - 2012): How do I replace a fields converter + fieldsettings dynamically using XAML?

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, ie 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM