WPF 设置Button的content为多行模式
查找button的子元素是个TextBlock,再设置它的TextWrappingProperty属性为 TextWrapping.Wrap。
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(btn, typeof(TextBlock)); o[0].SetValue(TextBlock.TextWrappingProperty, TextWrapping.Wrap); } /// /// 根据类型查找子元素 /// ////// /// /// public List GetChildObjects (DependencyObject obj, Type typename) where T : FrameworkElement { DependencyObject child = null; List childList = new List (); 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 (child, typename)); } return childList; }
原文:https://www.cnblogs.com/wgscd/p/15069704.html