简体   繁体   English

XAML中的StaticResource和DynamicResource

[英]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.

相关问题 我是否可以始终将DynamicResource替换为与Source = StaticResource的绑定 - Can I always replace DynamicResource with a Binding to a Source=StaticResource 没有使用DynamicResource作为参数从XAML设置DependencyPropert - DependencyPropert not set from XAML with DynamicResource as parameter Xamarin XAML StaticResource在另一个里面 - Xamarin XAML StaticResource inside another one 如何使用 StaticResource 在 XAML 中定义 DataContext - How to define DataContext in XAML using StaticResource 纯粹在xaml中连接文字和静态资源字符串 - Concatenate literals and staticresource string purely in xaml 组织Xaml-无法从其他xaml文件应用StaticResource - Organizing Xaml - Unable to apply StaticResource from different xaml file 通过XAML中的C#代码背后的StaticResource样式传递 - Passing through StaticResource style defined in XAML in C# codebehind 在运行时动态更改ThemeResource / StaticResource或Win8 XAML应用程序中的其他应用程序? - Dynamically change ThemeResource/StaticResource or other in Win8 XAML app at runtime? 如何以及在何处为Simple XAML窗口中的转换器创建StaticResource密钥? - How and Where to Create StaticResource Key for a Converter within the Simple XAML Window? 自定义控件的属性只能由 StaticResource 填充,而不能由 XAML 中定义的值填充 - Properties of custom control can only be filled by StaticResource but not with values defined in XAML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM