Note that there are some explanatory texts on larger screens.

plurals
  1. POChange a window style at runtime worked on VS2008, doesn't work on VS2010
    text
    copied!<p>Something that was working on VS2008 (framework 3.5) seems to not work on VS2010 (framework 4).</p> <p>I need to change the style of a window at runtime (user preference). In VS2008 this code was working:</p> <p>Window1.xaml</p> <pre><code>&lt;Window x:Class="StyleTest.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" ResizeMode="CanResizeWithGrip"&gt; &lt;Window.Style&gt; &lt;Style TargetType="{x:Type Window}"&gt; &lt;Setter Property="MinWidth" Value="400" /&gt; &lt;Setter Property="Width" Value="500" /&gt; &lt;Setter Property="MinHeight" Value="400" /&gt; &lt;Setter Property="SizeToContent" Value="Height" /&gt; &lt;/Style&gt; &lt;/Window.Style&gt; &lt;Grid&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>Window1.cs</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Xml; using System.Windows.Markup; using System.IO; namespace StyleTest { /// &lt;summary&gt; /// Interaction logic for Window1.xaml /// &lt;/summary&gt; public partial class Window1 : Window { public Window1() { InitializeComponent(); Loaded += new RoutedEventHandler(ObjDialog_Loaded); } void ObjDialog_Loaded(object sender, RoutedEventArgs e) { XmlDocumentFragment frag = new XmlDocument().CreateDocumentFragment(); frag.InnerXml = "&lt;Style TargetType=\"{x:Type Window}\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"&gt; " + " &lt;Setter Property=\"Height\" Value=\"200\" /&gt;" + " &lt;Setter Property=\"Width\" Value=\"200\" /&gt;" + "&lt;/Style&gt;"; XmlNode node = frag.FirstChild as XmlElement; Style style = LoadXaml(node.OuterXml) as Style; if (style != null) Style = style; UpdateLayout(); } private object LoadXaml(string xaml) { Exception ex = null; object o = LoadXaml(xaml, out ex); if (ex != null) throw ex; return o; } public static object LoadXaml(string xaml, out Exception exception) { try { ParserContext pc = new ParserContext(); pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation"); pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml"); pc.XmlnsDictionary.Add("l", "http://www.teradp.com/schemas/GN4/1/WinUI"); pc.XmlnsDictionary.Add("c", "clr-namespace:TeraDP.GN4.Common;assembly=Common"); exception = null; return XamlReader.Load(new MemoryStream(Encoding.UTF8.GetBytes(xaml)), pc); } catch (Exception ex) { exception = ex; } return null; } } } </code></pre> <p>When I run this code on Framework 3.5 the window is displayed with a size of 200x200. When I run this code on Framework 4 thw window is displayed with a size of 500x400</p> <p>The strangest thing is that if I add MinWidth and MinHeight to the style applied at runtime those attributes works correctly also in VS2010, while Width and Height seems to be ignored.</p> <p>Do someone has a solution for this problem?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload