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; } |
fffffffffffffffff
test red font.
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步