WPF动态更改窗口样式的代码隐藏 [英] WPF changing window style dynamically codebehind

查看:124
本文介绍了WPF动态更改窗口样式的代码隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我有多个希望动态更改的窗口样式.不幸的是,我遇到了问题,看来当新样式和旧样式的allowtransparency设置为true时,我会遇到异常.

如果将以下代码片段添加到WPF项目中,则可以轻松重现我的问题.

将样式添加到app.xaml

Hi all!

I have multiple windowstyles which I like to change dynamically. Unfortunately I faced problem, it seems that when new style and old one has allowtransparency set true I got exceptions.

You can easilly reproduce my problem if you add following snippets to WPF project.

add styles to app.xaml

<Style x:Key="MyDialog1" TargetType="{x:Type Window}" BasedOn="{x:Null}">
           <setter property="AllowsTransparency" value="True" />
           <setter property="WindowStyle" value="None" />
           <setter property="Template">
               <setter.value>
                   <controltemplate targettype="{x:Type Window}">
                       <grid>
                           <border borderbrush="Black" borderthickness="3" cornerradius="10">
                               <adornerdecorator>
                                   <contentpresenter margin="20" />
                               </adornerdecorator>
                           </border>
                       </grid>
                   </controltemplate>
               </setter.value>
           </setter>
       </Style>

       <Style x:Key="MyDialog2" TargetType="{x:Type Window}" BasedOn="{x:Null}">
           <setter property="AllowsTransparency" value="True" />
           <setter property="WindowStyle" value="None" />
           <setter property="Template">
               <setter.value>
                   <controltemplate targettype="{x:Type Window}">
                       <grid>
                           <border borderbrush="Green" borderthickness="3" cornerradius="10">
                               <adornerdecorator>
                                   <contentpresenter margin="20" />
                               </adornerdecorator>
                           </border>
                       </grid>
                   </controltemplate>
               </setter.value>
           </setter>
       </Style>



在window.xaml中,请参考windowstyle MyDialog1



In window.xaml refer windowstyle MyDialog1

Style="{DynamicResource MyDialog1}"



将Button添加到表单网格中并进行点击事件



Add Button into forms grid and make click event

<Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="Change Style" Click="change_style_Click"></Button>



最后,在click事件的代码隐藏中,将样式更改为MyDialog2(此处发生异常.)



And finally in codebehind in click event change the style to MyDialog2 (Here the exception occurs..)

private void change_style_Click(object sender, RoutedEventArgs e)
       {
           //Both methods will fail..
           //this.SetResourceReference(WindowStyleProperty, "MyDialog2");
           this.Style = (Style)FindResource("MyDialog2");
       }



我设法改变了例如.按钮使用以下方法可以显示背景颜色,但是更改窗口似乎微不足道.

希望你有主意:)

干杯!



I have managed to change eg. buttons Background color with following methods, but changing window seems to be trivial.

Hope you got idea :)

Cheers!

推荐答案

您的问题是,您试图在显示窗口后设置窗口的透明度.您不允许这样做(例外).我相信这是有充分理由的.

本质上,您将不得不考虑一些解决方法.一个简单的解决方案是不设置样式更改的透明度.由于您的两种样式都包含该样式,为什么不将其设置为没有样式呢?

我认为这只是测试代码,也许您想切换透明度功能.简单地说,在显示"窗口之后是不可能的.

您可以在窗口周围构建一个包装器,以捕获要打开模式的事件,然后关闭原始包装并使用新样式打开新包装.这样会产生切换的效果,但是您将重新构建窗户.
另一种选择是利用Hide.这将不允许您切换样式(也就是说,如果样式正在尝试设置透明性),但是您可以有一个透明窗口和一个非透明窗口.两者都可以绑定到相同的数据上下文,您将隐藏不活动的上下文.
Your issue is you are trying to set the transparency of the window after it is shown. You are not allowed to do that (by the exception). I am sure there is good reason for this.

Essentially you will have to think of some work arounds. A simple solution is to not set the transparency in the style change. Since both of your styles contain it, why not just have it set that way with out a style?

I would assume this is just test code and maybe you want to toggle the transparency feature. Simply put it is not possible after the window has been "Shown".

You could build a wrapper around the window that is catching the event you want to to switch modes on and then close the original and open a new one with the new style. This would give the effect of toggling but you would be re-building the windows.
Another option would be to take advantage of Hide. This will not allow you to switch the style (that is if the style is trying to set transparency), but you could have one transparent window and one non transparent. Both could be bound to the same data context and you would hide the one that is not active.


这篇关于WPF动态更改窗口样式的代码隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆