XamDataGrid(2011-2012):如何使用XAML动态替换字段转换器+字段设置? [英] XamDataGrid (2011 - 2012): How do I replace a fields converter + fieldsettings dynamically using XAML?

查看:75
本文介绍了XamDataGrid(2011-2012):如何使用XAML动态替换字段转换器+字段设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XamDataGrid,其中包含一些字段,包括这两个字段:

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>

由于需求的变化和时间的压力,我需要找到一种方法来根据存在的某些值(很可能是布尔型标志)来动态更改字段的转换器和EditorStyle.我看过使用样式选择器的一般示例,但是没有(如果可能的话)如何将样式选择器应用于XamDataGrid的元素.我最初的想法是使用样式选择器类,并为每种情况添加两种样式,即一种用于字段应使用DivideBy1000Converter + DecimalMWhStyle组合,另一种用于何时字段应使用空转换器和PercentageStyle,但是我被困住了,现在我不知道该如何最好地实现自己想要的目标.

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?

推荐答案

使用行为快速执行此操作.

只需在XAML代码中构建所有样式和字段布局,然后在行为中使用它们即可.只需看下面的代码:

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;

我正在运行时为网格生成fieldlayout.并在运行时使用" DataRecordCellAreaStyle "," CellValuePresenterStyle "," LabelPresenterStyle ".这样,您便可以灵活地轻松置换不同的样式/转换器,并且易于实现和更改.

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.

这篇关于XamDataGrid(2011-2012):如何使用XAML动态替换字段转换器+字段设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆