Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing and Modifying xaml elements with Reflection in C#
    primarykey
    data
    text
    <p>I have a model that has a number of attributes that may or may not be null, depending on if the data is available for those specific attributes on a remote server.</p> <p>I'm building a simple windows phone application that gives more easily accessible versions of this information on a mobile phone. I found that in the case that the properties of the model above are not set, the value doesn't show up (shows a blank), but the label still shows up. </p> <p>After considering a number of different alternatives for this (including massive amounts of if statements, ugh), I decided that if I set the name for specific attributes in the xaml to match the properties of the data model that I was looking at, I then in real time have the system analyze the properties of the data model, and elements of my xaml. If a property in my xaml matched the name of the property in my model, and the model was null, I could turn the visibility to collapsed. If the model was not null, make it visible. Thus I could have a clean, dynamic solution that would show only the data that is actually available. </p> <p>Here's the code</p> <pre><code>PropertyInfo[] properties = data.GetType().GetProperties(); foreach (PropertyInfo property in properties) { FieldInfo view = this.GetType().GetField(property.Name); if (view != null) { if (property.GetValue(data, null) == null) { object aView = view.GetValue(this); aView.GetType().GetProperty("Visibility").SetValue(aView, "Collapsed", null); } else { object aView = view.GetValue(this); aView.GetType().GetProperty("Visibility").SetValue(aView, "Visible", null); } } } </code></pre> <p>Unfortunately, I hit a snag. I can't figure out how to access the xaml elements. I've tried to use </p> <pre><code>this.GetType().GetProperties() this.GetType().GetFields() this.GetType().GetMembers() </code></pre> <p>To find the elements that I'm looking for, but they don't show up in any of those. Is there something I'm missing.</p> <p>Is there a better way to do this that is more beautiful?</p> <p>Thanks in advance for your help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload