【问题标题】:Binding ImageSource of ImageBrush in UserControl to CodeBehind将UserControl中ImageBrush的ImageSource绑定到CodeBehind
【发布时间】:2016-02-02 02:03:51
【问题描述】:

我创建了一个包含按钮的用户控件。 每次使用按钮时,我都想设置图像刷背景的图像源路径。 为此,我必须这样做: 我在后面的代码中创建了一个依赖属性字符串 MyPath。 在我写的用户控件的 xaml 中:

<Button>
   <Button.Background>
        <ImageBrush ImageSource={Binding MyPath}/> 
   </Button.Background>
</Button>

在我编写的 UserControl (xamk.cs) 的构造中

DataContext=this;

MainWindow在View目录里面,图片的路径是“ViewResources\Regular.png”

所以我在 MainWindow.xaml 中写道:

<vm:MyButton NyPath="Resources\Regular.png"/>

为什么它不起作用?

【问题讨论】:

  • 如果资源路径真的是“ViewResources\Regular.png”,你也应该在你的 XAML 中写下它。另外,确保镜像文件的 Build Action 设置为Resource

标签: wpf xaml data-binding user-controls


【解决方案1】:

您需要在 xaml 顶部设置 UserControl 的名称,然后像这样绑定它:

<Button>
  <Button.Background>
    <ImageBrush ImageSource={Binding MyPath, ElementName="YourUserControlName"}/> 
  </Button.Background>
</Button>

【讨论】:

    猜你喜欢
    • 2014-05-01
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多