【问题标题】:Lock data binding MVVM锁定数据绑定 MVVM
【发布时间】:2015-07-15 07:54:11
【问题描述】:

我有一个带有 Image 组件的表单。我想在运行时加载图像。 在我的 ViewModel 中,我有一个代表图像源路径的属性:

public string ImagePath { get; set; }

此属性绑定到我的 ImageComponent 的 Source。 问题是,当我启动我的应用程序时,ImagePath 为空,默认转换器尝试将 ImagePath 转换为 System.Windows.Media.ImageSource 并引发异常。

我想到了 3 个解决方案:
- 创建一个自定义转换器(当字符串为空时可以提供默认的 ImageSource)
- 阻止视图获取 ImagePath(不知道如何)
- 使用 System.Windows.Media.ImageSource 而不是字符串。 (不确定 MVVM 模式是否实现,因为 System.Windows.Media 仅由视图使用)

所以我的问题是:哪个解决方案更好(不仅是我的 3 个),实施方案是什么?

XAML 绑定:

 <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
      <Image Name="image" Stretch="Uniform" Source="{Binding Main.ImagePath, Mode=OneWay, Source={StaticResource Locator}}" />
 </ScrollViewer>

引发的异常:

System.Windows.Data Error: 23 : Cannot convert '' from type 'String' to type 'System.Windows.Media.ImageSource' for 'en-US' culture with default conversions;

【问题讨论】:

  • 你能添加你在 XAML 中使用的 Control + Binding 吗?
  • 我编辑了我的问题,谢谢
  • 嗯,很奇怪,无论我是否将 ImagePath 设置为 null""、有效或无效路径,我都不会收到该错误...可能是因为我不使用 ViewModelLocator ,我也试试
  • 确定是null吗?它看起来像一个空的 string (""),这可能解释了为什么 @Staeff 无法复制。
  • 他们都失败了,我尝试了""后null引发了我一个异常

标签: c# wpf mvvm binding


【解决方案1】:

我刚刚开始了一个普通的 MVVM Light 项目 (.NET 4.5),并将以下内容添加到我的代码中,但我没有收到您描述的错误。

MainViewModel:

public class MainViewModel : ViewModelBase
{
    public string ImagePath
    {
        get
        {
            return _imagePath;
        }
        set
        {
            _imagePath = value;
            RaisePropertyChanged(() => ImagePath);
        }
    }
    private string _imagePath;

    public RelayCommand ImageCommand
    {
        get
        {
            return _imageCommand ??
                (_imageCommand = new RelayCommand(() => ImagePath = "Image.png"));
        }
    }
    private RelayCommand _imageCommand ;

    public MainViewModel()
    {
        // I've tried both of those and it still works
        //ImagePath = "";
        //ImagePath = null;
    }
}

主窗口内容 XAML:

<StackPanel>
    <ScrollViewer HorizontalScrollBarVisibility="Auto"
                  VerticalScrollBarVisibility="Auto">
        <Image x:Name="TestImage"
               Source="{Binding Main.ImagePath,
                                Mode=OneWay,
                                Source={StaticResource Locator}}"
               Stretch="Uniform" />
    </ScrollViewer>

    <Button Command="{Binding Main.ImageCommand,
                              Source={StaticResource Locator}}"
            Content="Click" />
</StackPanel>

【讨论】:

    【解决方案2】:

    正如我在之前的评论中所说,您不需要自定义转换器来管理空值。您可以在源的绑定中使用TargetNullValue

    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
      <Image Name="image" Stretch="Uniform" Source="{Binding Main.ImagePath, TargetNullValue={x:Null}, Mode=OneWay, Source={StaticResource Locator}}" />
    </ScrollViewer>
    

    此外,如果您愿意,您可以在TargetNullValue 中指定默认路径。

    【讨论】:

      猜你喜欢
      • 2013-01-07
      • 1970-01-01
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-17
      • 2012-10-29
      相关资源
      最近更新 更多