locked
Simple user control RRS feed

  • Question

  • In my project I create a new User Control (MyControl) and I put in two TextBlock (Title and Year)

    From another page I would create a ListBox with this control

    MyControl example = new MyControl();

    but when I write

    example.

    doesn't appear Title and Year

    Why?

    Thanks


    Friday, December 28, 2012 10:54 PM

Answers

  • In my project I create a new User Control (MyControl) and I put in two TextBlock (Title and Year)

    From another page I would create a ListBox with this control

    MyControl example = new MyControl();

    but when I write

    example.

    doesn't appear Title and Year

    Why?

    Thanks


    You need to set access modifiers and names on the controls you want to access.

    i.e.

    <TextBox x:Name="Title" x:FieldModifier="public" />


    British Airways Inspiration App

    • Marked as answer by Pierluca Conti Saturday, December 29, 2012 7:46 PM
    Saturday, December 29, 2012 12:23 PM

All replies

  • You can try to access the child controls with the help of VisualTreeHelper. This method should return the control you need:

    public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
            {
                if (parent == null) return null;
                T foundChild = null;
                int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
    
                for (int i = 0; i < childrenCount; i++)
                {
                    var child = VisualTreeHelper.GetChild(parent, i);
                    T childType = child as T;
    
                    if (childType == null)
                    {
                        foundChild = FindChild<T>(child, childName);
                        if (foundChild != null) break;
                    }
                    else if (!string.IsNullOrEmpty(childName))
                    {
                        var frameworkElement = child as FrameworkElement;
                        if (frameworkElement != null && frameworkElement.Name == childName)
                        {
                            foundChild = (T)child;
                            break;
                        }
    
                        foundChild = FindChild<T>(child, childName);
                        if (foundChild != null) break;
                    }
                    else
                    {
                        foundChild = (T)child;
                        break;
                    }
                }
    
                return foundChild;
            }
    you can make a call to the above given utility like this:

    var myTextBlock = FindChild<TextBlock>(example, "myTextBlock");


    - Ram

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Edited by Ramprasath R Saturday, December 29, 2012 12:17 PM
    Saturday, December 29, 2012 12:16 PM
  • In my project I create a new User Control (MyControl) and I put in two TextBlock (Title and Year)

    From another page I would create a ListBox with this control

    MyControl example = new MyControl();

    but when I write

    example.

    doesn't appear Title and Year

    Why?

    Thanks


    You need to set access modifiers and names on the controls you want to access.

    i.e.

    <TextBox x:Name="Title" x:FieldModifier="public" />


    British Airways Inspiration App

    • Marked as answer by Pierluca Conti Saturday, December 29, 2012 7:46 PM
    Saturday, December 29, 2012 12:23 PM