简体   繁体   中英

Setting MaxWidth at runtime in WPF

This is a .Net 4.5 WPF question. I am trying to set the MaxWidth of a FrameworkElement defined in XAML (specifically, an Expander) programmatically, but I've tried

  • myFrameworkElement.MaxWidth = value
  • myFrameworkElement.SetValue(MaxWidthProperty,value) -and-
  • myFrameworkElement.Dispatcher.Invoke(() => MaxWidth = value);

No joy. When I run it through the debugger, MaxWidth stubbornly remains at the value specified at compile-time in the XAML. Is it even possible to set MaxWidth and MaxHeight at runtime?

Thanks.

Try to set the MaxWidth of your control in double in code. For example, for Textbox names txtTest you can set its maxwidth as below.

txtTest.MaxWidth=300.0;

In case of Expander as well this mechanism works properly. can you show me your xaml how you have defined your expander

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM