更新时间:2022-10-15 08:17:26
我称之为 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