将标签值绑定到值 [英] Binding a label value to a value
问题描述
我是WPF的新手。我有app.config的值。我想将标签绑定到此值。
< StackPanel Margin = 10 VerticalAlignment = Top >
< DockPanel VerticalAlignment = Center Margin = 10 >
< Label DockPanel.Dock = Left FontWeight = 粗体目标= {Binding}>连接到服务器:< / 标签 >
< / DockPanel >
说
var value =系统.Configuration.ConfigurationManager.AppSettings [ Key]
.ToString(); // 1.1.1.1
我是什么想要的是标签内容成为:
连接到服务器:1.1.1.1
我称之为 CBS(强制性结合综合症)。解决方案是:
labelKey.Content = value ;
// 我甚至要解释原因吗?
当然,您需要在标签声明的XAML行中添加属性Name =labelKey
:< Label Name =labelKey... />
。
参考文献:
http://msdn.microsoft.com/en-us/library/system.windows.controls.label%28v=vs.110%29.aspx [ ^ ],
http://en.wikipedia.org/ wiki / KISS_principle [ ^ ]。
绑定的真正目的是不同的。你可能想读一下这个主题。
我们有一个轶事流传着,其中一个国家有着名的辛辣,辛辣的美食:
一位旅行者在这种辛辣的美食方面遇到了麻烦,几乎无法容忍这么多辣椒。有一天,他坐在一家餐馆里,想着我能订购什么才能避免得到这么多辣椒?。他得到了一个叫做服务员的想法:
- 请,我想要煮鸡蛋。是的,只需煮沸,带壳;将全蛋放入贝壳中。
厨房在一两个小时内没有生产任何东西;人们开始担心。
- 需要这么长时间?
- 外出厨师是与所有人员进行头脑风暴:如何在这些鸡蛋中注入辣椒。如果你想煮鸡蛋,只需将它们煮沸。
-SA
I am pretty new to WPF. I have a value from app.config. I want to binding a label to this value.
<StackPanel Margin="10" VerticalAlignment="Top">
<DockPanel VerticalAlignment="Center" Margin="10">
<Label DockPanel.Dock="Left" FontWeight="Bold" Target="{Binding}">Connect to Server:</Label>
</DockPanel>
Say
var value = System.Configuration.ConfigurationManager.AppSettings["Key"]
.ToString(); //1.1.1.1
What I want is the label content become:
Connect to Server: 1.1.1.1
I call it "CBS" (Compulsory Binding Syndrome). The solution is:
labelKey.Content = value; // Do I even have to explain why?
Of course, you need to add the attributeName="labelKey"
in XAML line with label declaration:<Label Name="labelKey" ... />
.
References:
http://msdn.microsoft.com/en-us/library/system.windows.controls.label%28v=vs.110%29.aspx[^],
http://en.wikipedia.org/wiki/KISS_principle[^].
The real purposes of binding are different. You may want to read on the topic.
We had an anecdote circulating, about one of the countries with notoriously spicy, peppery cuisine:
One traveler had trouble with this peppery cuisine, barely able to tolerate so much pepper. One day, he was sitting in a restaurant, thinking "What can I order to avoid getting so much pepper?". He got an idea an called a waiter:
"— Please, I want boiled eggs. Yes, just boiled, unshelled; serve the whole eggs, in the shells."
The kitchen did not produce anything for an hour or two; people started to worry.
"— What takes so long?"
"— Out chef is brainstorming with all the personnel: how to inject pepper in those eggs."If you want boiled eggs, just boil them.
—SA
这篇关于将标签值绑定到值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!