0

I want to iterate all of controls of contentdialog if available.

because I want to get and set Tag Property of each controls in a contentdialog.

for example,

    public ContentDialog MyDialog = new ContentDialog
    {
        Title = "My Title",
        Content = "My Content",
        PrimaryButtonText = "OK",
        SecondaryButtonText = "Cancel",
    };

for example pseudo code,

void DeepFirstSearch(ContentDialog IN_pMyDialog, DependencyObject IN_pControl)
{
    foreach (pControl in IN_pMyDialog)
    {
       if ( pControl is TextBlock )
       {
         ...
       }
       else if ( pControl is Button )
       {
         ...
       }

       if (pControl.GetChildCount() > 0)
       {
         DeepFirstSearch(IN_pDialog, pControl)
       }
    }
}
user8977483
  • 147
  • 10

1 Answers1

0

There is no Visual Tree in a ContentDialog so there are no children. You want to iterate through its properties, you can do that with reflection.

var cd = new ContentDialog();
var cdProps = cd.GetType().GetProperties();
foreach (var propInfo in cdProps)
{
    if (typeof(Button).IsAssignableFrom(propInfo.PropertyType))
    {
        var button = (Button)(cd.GetType().GetProperty(propInfo.Name).GetValue(cd, null));
        // Do stuff with it
    }
    if (typeof(TextBlock).IsAssignableFrom(propInfo.PropertyType))
    {
        var textBlock = (TextBlock)(cd.GetType().GetProperty(propInfo.Name).GetValue(cd, null));
        // Do stuff with it
    }
}
Sean O'Neil
  • 1,222
  • 12
  • 22
  • Thanks for answering and it solved. i got an idea, however, that code not worked, so i modified a little. – user8977483 Dec 22 '17 at 08:07
  • 1
    public void Reflection(ContentDialog IN_pObject) { var cdProps = IN_pObject.GetType().GetProperties(); foreach (var propInfo in cdProps) { string strTypeName = propInfo.ToString().Split(' ')[0]; if (strTypeName == "System.String" || propInfo.Name == "Title" || propInfo.Name == "Content") { //TODO IN_pObject.GetType().GetProperty(propInfo.Name).SetValue("TODO"); } } } – user8977483 Dec 22 '17 at 08:08