【问题标题】:What makes this image display in my WPF usercontrol?是什么让这个图像显示在我的 WPF 用户控件中?
【发布时间】:2010-12-18 05:53:04
【问题描述】:

我一直在尝试制作一个简单的用户控件来显示图像,并最终让它在我的 WPF 应用程序中充当按钮。

我将这些用户控件之一写入表单的 XAML,并且始终显示图像。但是,当以编程方式将它们添加到堆栈面板时,控件在那里,但图像从未显示。

这是 UserControl:(简单,但适用于本示例)

<UserControl x:Class="ImgButton"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    MinHeight="32" MinWidth="32"
    x:Name="uc_ImgButton">
    <Border BorderBrush="Gray" BorderThickness="2">
        <Image Source="{Binding ElementName=uc_ImgButton, Path=Source}" x:Name="img"/>
    </Border>
</UserControl>

我将 Source 属性设置为 ImageSource 类型的依赖属性:

Partial Public Class ImgButton

    Public Property Source() As ImageSource
        Get
            Return GetValue(SourceProperty)
        End Get
        Set(ByVal value As ImageSource)
            SetValue(SourceProperty, value)
        End Set
    End Property

    Public Shared ReadOnly SourceProperty As DependencyProperty = _
                           DependencyProperty.Register("Source", _
                           GetType(ImageSource), GetType(ImgButton))

End Class

这是我如何在 VB 中以编程方式添加它们的示例:

Dim newBtn As New myApp.ImgButton
newBtn.Width = 100
newBtn.Height = 100
Dim bi As New BitmapImage
bi.BeginInit()
bi.UriSource = New Uri("C:\test.png", UriKind.RelativeOrAbsolute)
bi.EndInit()
'MsgBox(bi.Width)  '(a simple debug test I added)
newBtn.Source = bi
Me.StackPanelMain.Children.Add(newBtn)

这是奇怪的部分......如上所示的代码运行没有错误,但我的表单上的结果是一个空边框,里面没有显示图像。

但是,如果您取消注释 MsgBox 行,现在将显示图像。就好像强迫它从 BitmapImage 获得一些价值使它工作。我还尝试用“dim x as integer = bi.PixelWidth”之类的无害的东西替换 MsgBox 行,并且还使图像显示。如果我把它拿走,我的表格上就没有图像了。

我怀疑我遗漏了什么,或者只是不明白什么。我想了解发生了什么,而不是在我的应用中留下看似毫无意义的代码行。

【问题讨论】:

    标签: wpf vb.net image user-controls


    【解决方案1】:

    据我所知,您不需要用户控件。甚至没有样式按钮。只需将按钮的填充属性设置为图像画笔即可。一个图像按钮:)

    【讨论】:

      【解决方案2】:

      我不确定我是否理解您尝试设置的绑定。通常,当您使用 ElementName 时,这意味着您正在绑定到同一可视树中的另一个控件。但是,根据您的 ImgButton 类的代码示例,它看起来只是一个普通类。您确定要使用ElementName

      另外,我相信为了让一个类拥有DependencyProperty,它必须派生自DependencyObject。如果您不想进行该更改,您始终可以实现INotifyPropertyChanged 并在Source 属性的设置器中触发PropertyChanged 事件。

      更新:我认为部分问题是我误读了您的问题(我没有意识到 ImgButton 是您的 UserControl 课程)。

      无论如何,我不确定这里发生了什么。但是,如果您的属性是图像文件的路径,而不是图像本身呢?也许是这样的:

      XAML:

      <UserControl x:Class="ImgButton"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          MinHeight="32" MinWidth="32"
          x:Name="uc_ImgButton">
          <Border BorderBrush="Gray" BorderThickness="2">
              <Image>
                  <Image.Source>
                      <BitmapImage UriSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, Path=Source}" />
                  </Image.Source>
              </Image>
          </Border>
      </UserControl>
      

      C#:

      Partial Public Class ImgButton
      
          Public Property Source() As String
              Get
                  Return GetValue(SourceProperty)
              End Get
              Set(ByVal value As ImageSource)
                  SetValue(SourceProperty, value)
              End Set
          End Property
      
          Public Shared ReadOnly SourceProperty As DependencyProperty = _
                                 DependencyProperty.Register("Source", _
                                 GetType(String), GetType(ImgButton))
      
      End Class
      

      然后只需将Source 属性设置为图像的路径:

      Dim newBtn As New myApp.ImgButton
      newBtn.Width = 100
      newBtn.Height = 100
      newBtn.Source = "C:\test.png"
      Me.StackPanelMain.Children.Add(newBtn)
      

      我以前没有使用过BitmapImage 类,但它可能需要一些东西才能让它呈现图像。不过,我并不是你的代码不起作用的真正原因,但也许如果你通过 XAML 中的绑定设置源代码,它会更像你原来的情况,它会起作用。

      【讨论】:

      • 在我的 WPF 体验中,我的大部分工作都是基于我在网上找到的代码 sn-ps 来帮助我入门的。我只想要一个用户控件,它具有我可以设置的属性来更改其中显示的图像。如果有更好的方法来实现这一点,请免费分享,我想学习:-) 我不太明白你的建议......示例代码会有所帮助。
      • 感谢您提供额外的代码,我会尝试使用它。我一直难以理解应如何用 XAML 编写数据绑定语句,以及何时适合使用某些绑定方法。
      • 需要一段时间才能掌握它。在 VS2010 中,有标记扩展(包括绑定)的智能感知,所以希望这将有助于学习曲线。
      猜你喜欢
      • 2017-02-18
      • 2013-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-22
      相关资源
      最近更新 更多