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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 10:44:02

问题


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:


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.



来源:https://stackoverflow.com/questions/32958906/xamdatagrid-2011-2012-how-do-i-replace-a-fields-converter-fieldsettings-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!