锁数据绑定MVVM
Lock data binding MVVM
我有一个带有图像组件的表单。我想在运行时加载图像。
在我的 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;
我刚刚启动了一个 vanilla 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>
正如我在之前的评论中所说,您不需要自定义转换器来管理空值。您可以在源代码的绑定中使用 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
中指定默认路径。
我有一个带有图像组件的表单。我想在运行时加载图像。 在我的 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;
我刚刚启动了一个 vanilla 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>
正如我在之前的评论中所说,您不需要自定义转换器来管理空值。您可以在源代码的绑定中使用 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
中指定默认路径。