【问题标题】:WPF Binding Image SourceWPF 绑定图像源
【发布时间】:2016-01-05 16:45:28
【问题描述】:

也许是愚蠢的问题,但我不知道了......

我有这样的 ViewModel 类:

public class MainWindowsViewModel : INotifyPropertyChanged
{
    private ImageSource _img;
    public ImageSource StatusImage
    {
        get { return _img; }
        set
        {
            _img = value;
            NotifyPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName]String propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

XAML 中的绑定如下所示:

  <Window.DataContext>
    <VM:MainWindowsViewModel />
  </Window.DataContext>
    <Image x:Name="gui_image_status" HorizontalAlignment="Left" Height="26" Margin="144,10,0,0" VerticalAlignment="Top" Width="29" Source="{Binding Path=StatusImage}" />

我这样设置 ImageSource 的内容:

MainWindowsViewModel _view = new MainWindowsViewModel();

        var yourImage = new BitmapImage(new Uri(String.Format("Sources/{0}.png", "red"), UriKind.Relative));
        _view.StatusImage = yourImage;

但它不起作用。我认为问题在于NotifyPropertyChanged,因为我尝试在setget 中放置制动点。 Get 在开始时触发了几次,之后set 也触发了正确的 ImageSource,但之后get 不再触发。就像从未发生过任何设置一样。

真的很简单的绑定,我做过很多次类似的...我不知道为什么这次它不起作用。

【问题讨论】:

    标签: c# wpf xaml binding imagesource


    【解决方案1】:

    您正在创建 MainWindowsViewModel 类的两个实例,一个在 XAML 中由

    <Window.DataContext>
        <VM:MainWindowsViewModel />
    </Window.DataContext>
    

    一个在代码后面

    MainWindowsViewModel _view = new MainWindowsViewModel();
    

    因此,您的代码将属性设置在与视图绑定的不同的视图模型实例上。

    将后面的代码更改为:

    var viewModel = (MainWindowsViewModel)DataContext;
    viewModel.StatusImage = new BitmapImage(...);
    

    【讨论】:

    • 我认为这只是一个例子而不是真正的代码。我希望。
    • 你的意思是这个问题?这当然是真实的代码,也是一个常见的错误。我在 StackOverflow 上经常看到这种情况。
    • 应该通过 Application.Current.MainWindow.DataContext 之类的方式工作,但最好不要在 XAML 中创建视图模型实例,而是在后面的代码中创建一个全局可访问的实例,即在 App 类中。
    • 天哪,我很笨。但是我怎样才能从另一个类中获取 viewModel 的实例呢?像 MainWindow.DataContex.. 什么的。?
    • 我不知道为什么我没有像往常那样做。像这样:我在 Window.Resources 和 MainWindow.xaml.cs 中定义了 ViewModel,就像 MainWindowsViewModel = Resources["MainWindowsViewModel"] as MainWindowsViewModel; 我认为,这是最好的解决方案。
    【解决方案2】:

    我没有在您的代码中发现任何问题,但您可以尝试检查一些内容。

    1. 检查您的图像是否已添加到项目中,并将图像的构建操作设置为内容(如果较新则复制)。
    2. 在更新 ImageSource 之前调用 Freeze 方法以防止错误:“必须在与 DependencyObject 相同的线程上创建 DependencySource”

      var yourImage = new BitmapImage(new Uri(String.Format("Sources/{0}.png", "red"), UriKind.Relative));
      yourImage.Freeze();
      _view.StatusImage = yourImage;
      

    此外,还有一种更简单的方法可以在 WPF 中绑定图像。您可以使用字符串作为来源,并为绑定的属性设置资源路径:

    public string StatusImage  
    {
        get { return "/AssemblyName;component/Sources/red.png"; }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-19
      • 2013-01-11
      • 2017-10-09
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多