Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
模板绑定WPF中未解析相对路径_Wpf_Xaml_Dependency Properties_Templatebinding - Fatal编程技术网

模板绑定WPF中未解析相对路径

模板绑定WPF中未解析相对路径,wpf,xaml,dependency-properties,templatebinding,Wpf,Xaml,Dependency Properties,Templatebinding,我试图在WPF中创建一个图像按钮。我所做的是 创建一个从按钮继承的用户控件,并在其中声明一个应该具有所需映像的依赖项属性 在资源字典中声明它的xaml模板 将相对路径和完整路径传递给该依赖项属性,完整路径有效,但相对路径无效 用户控制类 public class ButtonWithImage : Button { public ImageSource ButtonImage { get { return (ImageSource)GetValue(Butt

我试图在WPF中创建一个图像按钮。我所做的是

  • 创建一个从按钮继承的用户控件,并在其中声明一个应该具有所需映像的依赖项属性
  • 在资源字典中声明它的xaml模板
  • 将相对路径和完整路径传递给该依赖项属性,完整路径有效,但相对路径无效
  • 用户控制类

      public class ButtonWithImage : Button
    {
    
        public ImageSource ButtonImage
        {
            get { return (ImageSource)GetValue(ButtonImageProperty); }
            set { SetValue(ButtonImageProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for ButtonImage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ButtonImageProperty =
            DependencyProperty.Register("ButtonImage", typeof(ImageSource), typeof(ButtonWithImage));
    
    
    }
    
    资源字典代码

        <Style x:Key="ButtonWithImageStyle" TargetType="complaintRegister:ButtonWithImage">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="complaintRegister:ButtonWithImage">
                    <StackPanel>
                        <Image Source="{Binding ButtonImage, RelativeSource={RelativeSource TemplatedParent}}" Width="50" Height="50"/>
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    我忍不住认为它正在尝试解析“resources/plus.png”而不是“resources/plus.png”。

    我打赌输出窗口中显示了绑定错误。:)

    你把那个错误信息贴在这里怎么样

    顺便说一句,在这种情况下,使用TemplateBinding而不是相对资源TemplatedParent更合适

    TemplateBinding就是这样说的,它被指定在控件的模板中很好地工作。:)

    请查看以下链接:

    编辑:

    啊,你说的是你的图像的位置

    默认情况下,图像控件仅在使用XAML指定其源属性时识别已编译的资源图像。但是,我们可以使用转换器或自定义标记扩展来实现这一目标。以下链接包含有关数据绑定转换器和标记扩展的信息

    因此,将png设置为build action->resource并重建解决方案或使用以下选项:

    <Image>
        <Image.Source>
            <BitmapImage UriSource="../Relative/Path/To/Image.png" />
        </Image.Source>
    </Image>
    
    
    
    不过,如果您希望在此基础上使用MVVM并根据数据更改路径,我建议您使用绑定:

    <Image Source="{Binding MyPath}" Height="50"... />
    
    
    
    我已经更新了问题中的错误,请检查一下。它从不运行,但会给出一个异常。嗨,你说的是图像位置。请阅读我的编辑。
    <Image>
        <Image.Source>
            <BitmapImage UriSource="../Relative/Path/To/Image.png" />
        </Image.Source>
    </Image>
    
    <Image Source="{Binding MyPath}" Height="50"... />