如何在 XAML 中定义变量? [英] How can I define a variable in XAML?

查看:57
本文介绍了如何在 XAML 中定义变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 XAML 中有以下两个按钮:

I have the following two buttons in XAML:

<Button Content="Previous"
        Margin="10,0,0,10"/>
<Button Content="Next"
        Margin="0,0,10,10"/>

如何将10"定义为一个变量,以便我可以在一个地方更改它,如下所示:

How can I define "10" to be a variable so I can change it in one place, something like this:

伪代码:

<variable x:key="theMargin"/>
<Button Content="Previous"
        Margin="{Variable theMargin},0,0,{Variable theMargin}"/>
<Button Content="Next"
        Margin="0,0,{Variable theMargin},{Variable theMargin}"/>

推荐答案

试试这个:

添加到 xaml 文件的头部

add to the head of the xamlfile

xmlns:System="clr-namespace:System;assembly=mscorlib"

然后将其添加到资源部分:

Then Add this to the resource section:

<System:Double x:Key="theMargin">2.35</System:Double>

最后,在边距上使用厚度:

Lastly, use a thickness on the margin:

<Button Content="Next">
   <Button.Margin>
      <Thickness Top="{StaticResource theMargin}" Left="0" Right="0"
                  Bottom ="{StaticResource theMargin}" />
   </Button.Margin>
</Button>

很多系统类型都可以这样定义:int、char、string、DateTime等

A lot of system types can be defined this way: int, char, string, DateTime, etc

注意:你是对的......不得不做一些更好的测试......更改为代码以便它应该工作

Note: You're right... Had to do some better testing.. changed to code so that it should work

这篇关于如何在 XAML 中定义变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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