有条件的资源创建WPF XAML设计/运行时间 [英] Conditional Resources Creation WPF XAML Design / Run time

查看:216
本文介绍了有条件的资源创建WPF XAML设计/运行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF级联绑定一个第一个问题,

我说过,我的更多的资源比预期中定义这两个在主窗口用户控件

这是很好见于史努比


  • 主窗口XAML 有描绘了2用户控件在一个主窗口视图模型实例的绿色的和的蓝色


  • additionnaly有每个用户控件2中,将油漆在小圈子用户控件的ViewModels的两个实例的红色


  1. 我的第一个意向在用户控件是能够看到生活的我的控制会是什么样子(我画的红色来区分这个样板绿色和蓝色圆圈主窗口)

  2. 我的资源是非常便宜的创造,所以无论我是否有仍然生活在我的应用程序2个额外的资源,但我预计时间在我的资源会更贵..

最后我的问题是:

我怎样才能conditionnaly让资源在低级别控制创建(这样我可以在控制的角度看,preVIEW)的 prevent此创建时运行完整应用程序(或主窗口的视图),因为我将绑定到这个上级的资源。

提前非常感谢。

最好的问候。

NGI


解决方案

我最终设法回答我的问题。

结果是期待已久的没有重复资源在较低的水平视图模型实例
结果在窥探:

在XAML code

 <用户控件X:类=BindingMCVE.UserControl1
         的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
         的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
         的xmlns:MC =htt​​p://schemas.openxmlformats.org/markup-compatibility/2006
         的xmlns:D =htt​​p://schemas.microsoft.com/ex$p$pssion/blend/2008
         XMLNS:地方=CLR的命名空间:BindingMCVE
         D:的DataContext ={D:DesignInstance {X:类型vm_nmspc:my_usercontrol_vm},IsDesignTimeCreatable = TRUE}
         MC:可忽略=D
         D:DesignHeight =100D:DesignWidth =100
         的xmlns:vm_nmspc =CLR的命名空间:BindingMCVE.ViewModel
 >< UserControl.Resources>
    <! - 没有在XAML中创建的资源 - >
< /UserControl.Resources>
        <网格和GT;
    < StackPanel的高度=100WIDTH =100>
        <椭圆填充={结合my_color的}HEIGHT =50WIDTH =50/>
    < / StackPanel的>< /网格和GT;

请注意新行:<​​/ P>


  

D:的DataContext ={D:DesignInstance {X:类型vm_nmspc:my_usercontrol_vm},IsDesignTimeCreatable = TRUE}


也绝对没有其他修饰!!

诀窍是使用IDE来支持你(的如果你不知道,这将导致在XAML语法的)。

我会尽量说明我做到了。

选择你的对象,并在格式菜单中选择设置设计时的DataContext

然后选择你DesignInstance(这里的视图模型my_usercontrol_vm 的)和不要忘记的滴答作响的 IsDesignTimeCreatable

这创建的魔术的在椭圆报关行,但我把它在XAML的顶部。

所以这个回答我的问题。这可能与WPF / XAML没有深厚的知识,使所有的子视图(对照组)渲染没有的ViewModels的资源浪费

的额外费用

要给予回音什么鲆给了一个答案,我还发现了另一篇文章(将尝试再次找到它,并在接下来的编辑引用它)直接使用功能的 DesignerProperties.GetIsInDesignMode()

 公共部分类的UserControl1:用户控件
{
    公众的UserControl1()
    {        的InitializeComponent();        如果(DesignerProperties.GetIsInDesignMode(本))
        {
            //你想在这里设计时的具体行动是什么
        }
    }
}

Following a first question on WPF Cascading Binding,

I remarked that I had more Resources than desired defined in both the MainWindow and the UserControls:

This is well seen in Snoop

  • the MainWindow XAML has an "instance" of a MainWindow ViewModel that paints the 2 usercontrol in green and blue

  • additionnaly there are 2 instances of usercontrol viewmodels for each of the 2 UserControls that would paint the inner circle in red

  1. My first Intention in the Usercontrol was to be able to see "live" how my control would look like ( I painted it red to distinguish this "model" from the green and blue circles in the Main Window)
  2. My Resources are very cheap to create, so no matter if I have 2 extra resources that are still living in my application, but I would anticipate the time where my Resources will be more expensive...

Finally my question is :

How can I conditionnaly let Resources be created in "low level" control ( so that I can have a preview in the view of the control ) but prevent this creation when running the full application ( or the view of the Main Window ) because I will bind to the Resources of this upper level.

Many thanks in advance.

Best Regards.

NGI

解决方案

I will finally try to answer my own question

Result is as awaited no "duplicate resources" in the lower level ViewModel instances The result in snoop:

The XAML code

<UserControl x:Class="BindingMCVE.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:BindingMCVE"
         d:DataContext="{d:DesignInstance {x:Type vm_nmspc:my_usercontrol_vm}, IsDesignTimeCreatable=True}"
         mc:Ignorable="d" 
         d:DesignHeight="100" d:DesignWidth="100"
         xmlns:vm_nmspc="clr-namespace:BindingMCVE.ViewModel"               
 >

<UserControl.Resources>
    <!--No resources created in XAML-->
</UserControl.Resources>


        <Grid>
    <StackPanel  Height="100"  Width="100">
        <Ellipse  Fill="{Binding my_color}" Height="50" Width="50" />
    </StackPanel>

</Grid>

Note the new line:

d:DataContext="{d:DesignInstance {x:Type vm_nmspc:my_usercontrol_vm}, IsDesignTimeCreatable=True}"

And there is absolutely no other modifications !!

The trick is to use the IDE to support you ( if you are unaware of the syntax that will result in the XAML ).

I will try to illustrate what I did.

Choose your object, and in the format menu choose Set Design Time DataContext

Then choose your DesignInstance ( here the ViewModel my_usercontrol_vm) and do not forget to tick IsDesignTimeCreatable

This created the magic line in the Ellipse declaration but I moved it on the top of the XAML.

So this answered my question. It is possible with no deep knowledge on WPF/XAML to make all your subviews (controls) "rendered" without the additional cost of wasted resources of viewmodels

To give echo to what Maximus gave in the first answer, I found also another post (will try to find it again and quote it in a next edit) using directly the function DesignerProperties.GetIsInDesignMode( )

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {

        InitializeComponent();

        if (  DesignerProperties.GetIsInDesignMode(this))
        {
            //Do what you want here for design time specific actions
        }
    }
}

这篇关于有条件的资源创建WPF XAML设计/运行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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