Ticket T601244
Visible to All Users
Duplicate

We have closed this ticket because another page addresses its subject:

How to change a DetailView editor or ListView column caption dynamically

How to change Property caption at runtime based on another property value

created 7 years ago

Hi,
I am trying to change property captions at runtime based on another propertys value.
Simple i have a Master object with single property named 'Year'.
And i have a non persistent object which has a reference to master object.
In that non persistent object there are properties named 'Month1', Month2', … and so on.

I want to change those properties captions to 2017 / January, 2017 / February … and so on when the NonPersistentObject.MasterObject property changed.

I have attached a simple project that shows my issue and how i did try to do the required task.

To see problem on sampel;
1- Run the program.
2- Select first (2016) record on the MasterObject ListView
3- Click the SAmple Action to invoke the Non Persistent object's DetailView.
4- See that Month Captions didnt change. They are : MONTH 1, MONTH 2, MONTH 3 and so on…
5- Dont close the detailview and SELECT the 2017 master object. See that the captions changed like 2016 / JANUARY, 2016 / FEBRUARY … and so on. This is wrong they were must be the initial caption when the detailview shown.
6. Dont close the detailview and SELECT the 2018 master object. Note that the captions didnt change again.
7- Dont close the detailview and SELECT the 2016 master object. See that the captions changed like 2017 / JANUARY, 2017 / FEBRUARY … and so on

I am trying to achive my goal in ViewController_ItemCreated and ObjectSpace_ObjectChanged events in the Module.Web  NPSampleNonPersistenObject_VCD viewcontroller. You can check the code for more details.

Any help will appreciate.

Thanks.
Akın GÜNEŞ

Comments (2)
DevExpress Support Team 7 years ago

    Hi Akın,

    Thank you for the sample project. Please give us additional time to research it. We will get back to you as soon as possible.

      Hi Uriah,
      Thank you for your answer.
      I'll be waiting for your answer.

      Answers approved by DevExpress Support

      created 7 years ago (modified 7 years ago)

      Hello Akin,

      I can see that you tried to use the Frame.SetView method, as shown in our documentation, but apparently could not make this code work. I have debugged it and found several issues:
      - the view.GetItems<ASPxDecimalPropertyEditor>() method does not return any item because the actual type of these property editors is ASPxIntPropertyEditor;
      - there is an IndexOutOfRange exception when getting data from the MonthNames array since the month index should be zero-based;
      - it is necessary to avoid this code execution when the controller is activated after the Frame.SetView method is called.
      I have fixed these issues and have managed to make this solution work:

      C#
      using DevExpress.ExpressApp; using DevExpress.ExpressApp.Web.Editors.ASPx; using SampleLabelChange.Module.BusinessObjects; namespace SampleLabelChange.Module.Web.Controllers { // For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppViewControllertopic.aspx. public partial class NPSampleNonPersistenObject_VCD : ViewController<DetailView> { string[] MonthNames = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; public NPSampleNonPersistenObject_VCD() { InitializeComponent(); } protected override void OnActivated() { base.OnActivated(); UpdateCaptions(); this.View.ObjectSpace.ObjectChanged += ObjectSpace_ObjectChanged; } private void ObjectSpace_ObjectChanged(object sender, ObjectChangedEventArgs e) { if (e.Object == View.CurrentObject && e.PropertyName == "MasterObject" && e.NewValue != e.OldValue) { UpdateCaptions(); } } bool captionsUpdating = false; private void UpdateCaptions() { if (!captionsUpdating) { captionsUpdating = true; try { DetailView view = Frame.View as DetailView; var currentObject = view.CurrentObject as NPSampleNonPersistentObject; if (currentObject == null) return; if (Frame.SetView(null, true, null, false)) { foreach (var item in view.GetItems<ASPxIntPropertyEditor>()) { if (item.PropertyName.StartsWith("Month")) { int month = int.Parse(item.PropertyName.Substring(5)); item.Caption = currentObject.MasterObject.Year + " / " + MonthNames[month - 1]; } } view.LoadModel(false); Frame.SetView(view); } } finally { captionsUpdating = false; } } } protected override void OnDeactivated() { base.OnDeactivated(); this.View.ObjectSpace.ObjectChanged -= ObjectSpace_ObjectChanged; } } }

      Please let me know if you encounter any difficulty with this approach.

        Comments (3)

          Hi Anatol,
          Sorry for inconvenience and thank you for your effort and answer.
          Yes i saw those 2 problems in my code and fixed them.
          And also i have managed to find another solution with help of your SC.

          Here is my solution to help in case others have a similar issue.

          C#
          void ObjectSpace_ObjectChanged(object sender, ObjectChangedEventArgs e) { var currentObject = this.View.CurrentObject as NPSampleNonPersistentObject; if (currentObject != null) { if (e.PropertyName == "MasterObject" && currentObject.MasterObject != null) { foreach (var item in View.GetItems<ASPxIntPropertyEditor>()) { if (item.PropertyName.StartsWith("Month")) { int month = int.Parse(item.PropertyName.Substring(5)); SetItemCaption(item, currentObject.MasterObject.Year + " / " + MonthNames[month -1]); } } } } } private void SetItemCaption(ViewItem viewItem, string caption) { var c1 = viewItem.Control as System.Web.UI.Control; if (c1 == null) return; var c2 = c1.NamingContainer as LayoutItemTemplateContainer; if (c2 == null) return; if (c2.CaptionControl == null) return; if (c2.CaptionControl.Controls.Count == 0) return; var c3 = c2.CaptionControl.Controls[0]; if (c3 == null) return; var c4 = c3 as System.Web.UI.WebControls.Literal; if (c4 == null) return; c4.Text = caption; } protected override void OnViewControlsCreated() { base.OnViewControlsCreated(); var currentObject = this.View.CurrentObject as NPSampleNonPersistentObject; if (currentObject != null) { foreach (var item in View.GetItems<ASPxIntPropertyEditor>()) { if (item.PropertyName.StartsWith("Month")) { int month = int.Parse(item.PropertyName.Substring(5)); SetItemCaption(item, currentObject.MasterObject.Year + " / " + MonthNames[month - 1]); } } } }

          Thanks.

          Anatol (DevExpress) 7 years ago

            Thank you for informing us about your progress. I am happy to hear that the issue is resolved. Please do not hesitate to contact us if you need any assistance in the future.

            Dennis Garavsky (DevExpress) 7 years ago

              I feel like you might get some implementation ideas from the How to change a DetailView editor or ListView column caption dynamically thread and linked articles. I hope this information will also help other XAF community members.

              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.