Ticket Q500155
Visible to All Users

Formatting depending on another field value

created 12 years ago

Hello

In my XAF application I have defined some persistent object. I want one of its fields to be atypically formatted everywhere it's displayed (it's read only), so on all lists and detail views. I implemented custom formatter as described in documentation , then I wrote property editor and assigned it to field in BOModel->MyObject->OwnMembers->MyField->PropertyEditorType.

C#
[PropertyEditor(typeof(Int16), false)] public class MyPropertyEditor : DXPropertyEditor { public MyPropertyEditor(Type objectType, IModelMemberViewItem model) : base(objectType, model) { } protected override object CreateControlCore() { return new SpinEdit(); } protected override RepositoryItem CreateRepositoryItem() { return new RepositoryItemSpinEdit(); } protected override void SetupRepositoryItem(RepositoryItem item) { base.SetupRepositoryItem(item); item.DisplayFormat.FormatType = FormatType.Custom; item.DisplayFormat.Format = new MyFormatter(); item.DisplayFormat.FormatString = MyFormatter.MY_FORMAT; } }

This works fine, but the problem appears when I want to use different formatting depending on another field value. I found that for DetailView I can use CurrentObject property:

C#
int anotherField = ((MyObject)CurrentObject).AnotherField; switch (anotherField) { case 0: item.DisplayFormat.FormatString = MyFormatter.MY_FORMAT_A; break; case 1: item.DisplayFormat.FormatString = MyFormatter.MY_FORMAT_B; break; default: item.DisplayFormat.FormatString = MyFormatter.MY_FORMAT_C; break; }

But this doesn't work with ListView - CurrentObject is null. Is there some way to get my solution working also on lists, or some totally different approach is more suitable here?

I use DevExpress 10.1.8.

Karol

Answers approved by DevExpress Support

created 12 years ago (modified 12 years ago)

Hello Karol,
It is difficult to make this work in the ListView, because the ListView uses a single RepositoryItem's instance to draw editors in all rows. It is better to change formatting from the ListView side, e.g. in a ViewController. For example, you can use the GridView.CustomColumnDisplayText event to pass the required text directly to the grid cell. You can also use the GridView.CustomRowCellEdit event to provide editors with different settings for different rows. Please refer to the Access Editor Settings topic to learn how to use these events in XAF.

    Other Answers

    created 12 years ago (modified 12 years ago)

    Hello Anatol

    Thank you very much for helpful tip. I created view controller, in its properties I set TargetObjectType to MyObject, TargetViewType to ListView. This is my working solution:

    C#
    public partial class MyObjectListViewController : ViewController<ListView> { private MyFormatter formatter; public MyObjectListViewController() { InitializeComponent(); RegisterActions(components); } protected override void OnViewControlsCreated() { base.OnViewControlsCreated(); GridListEditor editor = View.Editor as GridListEditor; if (editor == null) { return; } formatter = new MyFormatter(); editor.GridView.CustomColumnDisplayText += GridView_CustomColumnDisplayText; } private void GridView_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs args) { if (args.Column.FieldName != "MyField") { return; } MyObject myObject = ((XafGridView)sender).GetRow(args.RowHandle) as MyObject; string format = ""; switch (myObject.AnotherField) { case 0: format = MyFormatter.MY_FORMAT_A; break; case 1: format = MyFormatter.MY_FORMAT_B; break; default: break; } if (format.Length > 0) { args.DisplayText = formatter.Format(format, args.Value, null); } } }

    Greetings
    Karol

      Comments (1)
      Anatol (DevExpress) 12 years ago

        You are welcome!

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.