visual-studio 如何绑定动态资源并指定路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3599134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:23:53  来源:igfitidea点击:

How to bind with dynamic resource and specifying a path

visual-studioxamlbindingdynamicresource

提问by erikH

I want to bind to a resource (DynamicResource) and access properties on that resource, but is there a way to do that?

我想绑定到资源 (DynamicResource) 并访问该资源上的属性,但有没有办法做到这一点?

(I want to visualize the default values from constructor in the xaml editor in visual studio. Those cannot be seen when referencing an object through DataContext nor through a property added on my Window class...)

(我想在 Visual Studio 的 xaml 编辑器中可视化构造函数中的默认值。通过 DataContext 或通过添加到我的 Window 类中的属性引用对象时无法看到这些值...)

Not working xaml:(works in composer but not at runtime...)

不工作 xaml :(在作曲家中工作但在运行时不工作......)

<Window ... >
    <Window.Resources>
        <local:MyClass x:Key="myResource"  />
    </Window.Resources>
    <StackPanel>
        <Button Content="{Binding Source={DynamicResource myResource} Path=Property1}" />
        <Button Content="{Binding Source={DynamicResource myResource} Path=Property2}" />
    </StackPanel>
</Window>

with the class (which probably need to implement INotifyPropertyChanged):

与类(可能需要实现 INotifyPropertyChanged):

public class MyClass 
{
    public MyClass()
    {
        this.Property1 = "Ok";
        this.Property2 = "Cancel";
    }
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

回答by Thomas Levesque

That's because the DynamicResourcemarkup extension can only be used on a dependency property, because it will need to update it if the resource changes. And Binding.Sourceis not a dependency property...

这是因为DynamicResource标记扩展只能用于依赖属性,因为如果资源发生变化,它需要更新它。并且Binding.Source不是依赖属性...

As a workaround, you could set the DataContextof the button with the DynamicResource:

作为一种解决方法,您可以使用以下命令设置DataContext按钮的DynamicResource

<Button DataContext="{DynamicResource myResource}" Content="{Binding Path=Property1}" />
<Button DataContext="{DynamicResource myResource}" Content="{Binding Path=Property2}" />

回答by Patrick Kursawe

Abusing the DataContext of an unrelated object seems to be the easiest workaround. In case you still need the DataContext of your control (MVVM anyone?), you can also create an invisible helper FrameworkElement elsewhere:

滥用不相关对象的 DataContext 似乎是最简单的解决方法。如果您仍然需要控件的 DataContext(MVVM 任何人?),您还可以在其他地方创建一个不可见的帮助程序 FrameworkElement:

<FrameworkElement Visibility="Collapsed" x:Name="ControlBrushGetter"  DataContext=" 
{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />

and later refer to it by using the name in the binding:

然后在绑定中使用名称来引用它:

<SolidColorBrush Opacity="0.8" 
Color="{Binding ElementName=ControlBrushGetter, Path=DataContext.Color}" />

Your designer will quite likely complain about not being able to resolve "Color" in the context of "object", but it will work fine at runtime.

您的设计师很可能会抱怨无法在“对象”的上下文中解析“颜色”,但它会在运行时正常工作。