简体   繁体   English

图片来源上的WPF位图效果错误

[英]WPF Bitmap effect error on image source

What I am trying to do is use the Imagebox as source for my bitmap effect and i dont know how to do that.My imagebox is called image1 . 我想做的是将Imagebox用作位图效果的源,但我不知道该怎么做。我的imagebox称为image1

<Button Content="Blur" Height="23" HorizontalAlignment="Left" Margin="148,12,0,0"    Name="button3" VerticalAlignment="Top" Width="42" Click="button3_Click" >
        <Image Source ="image1">
        <Image.BitmapEffect>
        <BlurBitmapEffect Radius="5" />
        </Image.BitmapEffect>
        </Image>
    </Button>

Are you using MVVM ? 您正在使用MVVM吗? If not, I highly recommend using this pattern because WPF is built to use it and if you don't, you will fight it all the way. 如果没有,我强烈建议您使用此模式,因为WPF是为使用它而设计的,如果您不使用WPF,则将一直使用它。

Create a ViewModel class. 创建一个ViewModel类。 Create a public property of type Image in this ViewModel class. 在此ViewModel类中创建Image类型的公共属性。

Create an instace of the ViewModel and put it into your Window's datacontext. 创建ViewModel的实例并将其放入Window的datacontext中。 Then add a binding to this property. 然后将绑定添加到此属性。

Alternatively for a quick fix (please note that this leads to darkness, you will regret having started to program this way): 另外 ,为了快速解决(请注意,这会导致黑暗,您将后悔以这种方式编程):

<Image Source="{Binding Source, ElementName=image1}"> 

Edit: 编辑:

Your edit is a completely different story: You have set the Content property twice: once by directly setting it and once by having a child object. 您所做的编辑是一个完全不同的故事:您已经设置了Content属性两次:一次是通过直接设置它,一次是通过拥有一个子对象。 Your button has both a text and an image as content. 您的按钮同时具有文本图像作为内容。 But a button (and most other controls) can only have one content. 但是一个按钮(和大多数其他控件)只能具有一个内容。 If you want both, your content needs to be a container control like a StackPanel that can have multiple contents and you need to put both your Image and your TextBlock in there. 如果两者都需要,则您的内容需要是一个像StackPanel这样的容器控件,它可以具有多个内容,并且需要将ImageTextBlock放在其中。

Example (you need to put in Orientation and Alignment as you see fit): 示例(您需要根据需要放入“方向和对齐”):

<Button Height="23" HorizontalAlignment="Left" Margin="148,12,0,0" Name="button3" VerticalAlignment="Top" Width="42" Click="button3_Click"> 
  <StackPanel>
    <TextBlock Text="Test"/>
    <Image Source="{Binding Source, ElementName=image1}">
      <Image.BitmapEffect>
        <BlurBitmapEffect Radius="5" />
      </Image.BitmapEffect>
    </Image>
</Button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM