[WPF]将变量放到xaml [英] [WPF] Place variable to xaml

查看:253
本文介绍了[WPF]将变量放到xaml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



如何在XAML上添加变量?从模块中检索的变量(modGlobalVar.vb)并将其放在XAML上?



因为我想把它放在矩形的imagesource路径上。示例: -

Hi all,

How to put a variable on XAML? A variable which retrieve from the module(modGlobalVar.vb) and place it on XAML?

Because I want to put it on rectangle imagesource path. Sample:-

<Rectangle Height="180" Width="50"
           Canvas.Top="5" Canvas.Left="5"
           Stroke="Black" StrokeThickness="1" >
    <Rectangle.Fill>
        <ImageBrush ImageSource="c:\image.jpg" Stretch="UniformToFill"/>
    </Rectangle.Fill>
</Rectangle>



就像这样: -


to be as like this for example:-

<Rectangle Height="180" Width="50"
           Canvas.Top="5" Canvas.Left="5"
           Stroke="Black" StrokeThickness="1" >
    <Rectangle.Fill>
        <ImageBrush ImageSource=strimagepath Stretch="UniformToFill"/>
    </Rectangle.Fill>
</Rectangle>



其中strimagepath是从整个解决方案中使用的变量中检索的


where the strimagepath is retrieve from the variable which use in the entire solutions

推荐答案

所有错误。你不明白如何使用XAML。您不修改用于定义UI的XAML,它不是运行时对象。



这是发生的事情。在构建期间,XAML(可能不是所有XAML,我的意思是你用来定义词典,用户控件,窗口等的XAML)被编译成代码(C#,VB.NET,无论如何)并且代码被传递到编译器。如果搜索项目的项目目录的子目录,您想知道它是什么。我建议你这样做,以了解事情是如何运作的。然后使用所有源文件编译这些文件,然后获得程序集。你的XAML在哪里?转换为程序代码。但是,此自动生成的代码不被视为真正的源代码,因此您可以将其删除,不应将其放在修订控制数据库下,依此类推。而不是这个代码,用于生成的XAML用作真正的源代码。



然后,在运行时,使用此生成的代码。最终,您的窗口(或其他任何东西)的实例将托管您的 Rectangle 以及其他所有内容。



那么该怎么办?与构建过程相同:忘记XAML,它不再存在。但是您的UI对象确实存在,因此您必须修改它们,例如,您的 Rectangle 实例。最简单的(并不总是最推荐的方法)是使用 Name XAML元素属性为其命名。将生成源代码中的名称,因此您可以在后面的代码中使用它。



另一种方法是遍历逻辑您的UI(窗口或其他任何内容)的树并到达目标UI元素。请参阅:

https:// msdn .microsoft.com / zh-CN / library / ms753391%28v = vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/ms753391%28v=vs.110%29.aspx #logical_tree [ ^ ]。



最后,重要的是,您可以在XAML中修改所需的UI对象。并不是说您的代码修改了XAML,而是XAML描述了修改UI对象的代码。我不知道你要修改什么事件,所以我不能告诉你的方式,但也许你想绑定一些属性与其他一些属性,你可以使用依赖属性。或者您可能需要使用触发器。这些是您可能需要学习的主题;使用这些关键字向MSDN发送地址。



-SA
All wrong. You don't understand how XAML is used. You don't modify XAML used to define you UI, it is not a runtime object.

Here is what happens. During build, XAML (not all XAML, perhaps; I mean just the XAML you used to define your dictionaries, user controls, windows, and so on) is compiled into code (C#, VB.NET, whatever) and that code is passed to the compiler. You want to find out what it is, if you search sub-directories of your project directories of your project. I would recommend you to do so, to understand how things work. Then these files are compiled with all your source files and you get the assembly. Where is your XAML? Converted to program code. However, this auto-generated code is not considered as a true source code, so you can delete it, you should not put it under your revision control database, and so on. Instead of this code, XAML used to generated is used as the true source code.

Then, during runtime, this generated code is used. Eventually, the instance of your window (or whatever else) hosting your Rectangle and everything else is created.

So, what to do? The same as your build process does: forget XAML, it does not exist anymore. But your UI objects do exist, so you will have to modify them, for example, your Rectangle instance. The simplest (and not always the most recommended way) would be giving it a name using Name XAML element attribute. The name in the source code will be generated, so you will be able to use it in the code behind.

Another approach would be traversing down the logical tree of your UI (window or whatever else) and reaching the target UI elements. Please see:
https://msdn.microsoft.com/en-us/library/ms753391%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/ms753391%28v=vs.110%29.aspx#logical_tree[^].

And finally and importantly, you can modify the required UI objects in XAML itself. It's not that your code modifies XAML, it's that XAML describes the code modifying your UI objects. I don't know on what events you want to modify it, so I cannot tell you the way, but perhaps you want to bind some properties with some other properties, which you can do using dependency properties. Or you may need to use triggers. Those are topics you may need to learn; address to MSDN using these keywords.

—SA


这篇关于[WPF]将变量放到xaml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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