WPF 创建一种“变量"在 xaml 代码中以便在其余的 xaml 代码中使用 [英] WPF Create a kind of "variable" in xaml code in order to use in the rest of xaml code
本文介绍了WPF 创建一种“变量"在 xaml 代码中以便在其余的 xaml 代码中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道在 xaml
中是否有可能创建一个种类"的变量,然后将它用于其余的 xaml 代码.例如,假设许多控件共享相同的宽度,那么我可以在 xaml 中声明一次宽度,然后在所有 xaml 代码中使用它吗?
I wonder whether in xaml
there is the possibility to create a "kind" of variable and then use it for the rest of xaml code. For example, suppose many controls share the same width so can I declare width once in xaml and then use it in all the xaml code?
推荐答案
您可以在资源中定义变量",但我更喜欢使用样式,将属性设置为相同的值:
You can define your "variables" in resources, but I would prefer to use styles, to set Properties to same value:
xmlns:Sys="clr-namespace:System;assembly=mscorlib"
<Window.Resources>
<Sys:Double x:Key="yourVar">30.0</Sys:Double>
</Window.Resources>
<ComboBox>
<ComboBox.Items>
<ComboBoxItem FontSize="{Binding Source={StaticResource yourVar}}">1</ComboBoxItem>
<ComboBoxItem>2</ComboBoxItem>
<ComboBoxItem>3</ComboBoxItem>
</ComboBox.Items>
</ComboBox>
这篇关于WPF 创建一种“变量"在 xaml 代码中以便在其余的 xaml 代码中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文