弹来弹去跑马灯!

WPF 设置Button的content为多行模式

查找button的子元素是个TextBlock,再设置它的TextWrappingProperty属性为 TextWrapping.Wrap。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Button btn2 = new Button() {Content="fdhfhfhfhfghfhfhfhfhhfh",Width=88};
          btn2.Loaded += Btn2_Loaded;
          gridRoot.Children.Add(btn2);
 
 
      }
 
      private void Btn2_Loaded(object sender, RoutedEventArgs e)
      {
          Button btn = (sender as Button);
          var o = GetChildObjects<TextBlock>(btn, typeof(TextBlock));
          o[0].SetValue(TextBlock.TextWrappingProperty, TextWrapping.Wrap);
 
      }
 
     
 
      /// <summary>
      /// 根据类型查找子元素
      /// </summary>
      /// <typeparam name="T"></typeparam>
      /// <param name="obj"></param>
      /// <param name="typename"></param>
      /// <returns></returns>
      public List<T> GetChildObjects<T>(DependencyObject obj, Type typename) where T : FrameworkElement
      {
          DependencyObject child = null;
          List<T> childList = new List<T>();
 
          for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
          {
              child = VisualTreeHelper.GetChild(obj, i);
 
              if (child is T && (((T)child).GetType() == typename))
              {
                  childList.Add((T)child);
              }
              childList.AddRange(GetChildObjects<T>(child, typename));
          }
          return childList;
      }

  

posted @   wgscd  阅读(611)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示