[英]StaticResource and DynamicResource in XAML
I am currently browsing WPF examples on this GitHub. 我目前正在浏览这个 GitHub上的WPF示例。 This one is a simple application - it binds a textbox to a label.
这是一个简单的应用程序 - 它将文本框绑定到标签。 Whenever the textbox's text changes, the label is changed too via a data binding.
每当文本框的文本发生变化时,标签也会通过数据绑定进行更改。
Now, I'm reading a StaticResource is evaluated the moment the XAML object is constructed and cannot be changed later, while a DynamicResource is evaluated each time it is accessed. 现在,我正在读取一个StaticResource在构建XAML对象时被评估,并且以后无法更改,而每次访问时都会评估DynamicResource。
In this program, it would seem to me DynamicResources should be used, however only two StaticResources are present. 在这个程序中,我觉得应该使用DynamicResources,但是只有两个StaticResources存在。
Here is what I'd deem the crucial code: 以下是我认为的关键代码:
// ...
<Window.Resources>
<local:Person x:Key="MyDataSource" PersonName="Joe"/>
</Window.Resources>
// ...
<TextBox>
<TextBox.Text>
<Binding Source="{StaticResource MyDataSource}" Path="PersonName"
UpdateSourceTrigger="PropertyChanged"/>
</TextBox.Text>
</TextBox>
<Label>The name you entered:</Label>
<TextBlock Text="{Binding Source={StaticResource MyDataSource}, Path=PersonName}"/>
How come it works with StaticResources? 它如何与StaticResources一起使用?
As stated by Clemens, 正如克莱门斯所说,
The object referenced by StaticResource MyDataSource does not change, just the value of its PersonName property.
StaticResource MyDataSource引用的对象不会更改,只会更改其PersonName属性的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.