how to get the children of an element WPF

前端 未结 2 1694
栀梦
栀梦 2020-12-09 20:36

I have a stackpanel that contains an Image and a TextBlock. I\'m raising an event once double click is being performed by the user.(P.S - I\'m adding the St

相关标签:
2条回答
  • You need to DataBind TextBlock Text (?) element to your class - like so:

    In XAML

    <TextBlock x:Name="MyTextBlock"
       Text={Binding ShowThis, Mode=OneWay} />
    

    in class:

     public class MyDataContextClass
     {
         private string showThis = string.Enpty;
         public string ShowThis
         {
             get {return showThis;}
             set
             {
                  showThis = value;
                  if (PropertyChanged != null)
                      PropertyChanged(....);
             }
          }
      }
    

    and you must DataBing Xaml to class. (May be in constructor ?)

      public class MyXamlWindow
      {
           public MyXamlWindow()
           {
                 this.DataContext = new MyDataContextClass();
           }
       }
    

    There's a lot of ways to do all above

    0 讨论(0)
  • 2020-12-09 21:00

    A simple way of getting the first child element of a certain type (e.g. TextBlock) is this:

    var textBlock = panel.Children.OfType<TextBlock>().FirstOrDefault();
    

    You either get the first TextBlock or null if there isn't any.

    0 讨论(0)
提交回复
热议问题