我是WPF和MVVM的新手。当前使用MVVM模式构建WPF应用程序,但很快就陷入了多个MVVM的困境。
基本上,我有一个MainWindow和MainWindow上的一个UserControl。这是一个简单的UserControl,仅包含一个Image Control和一个滚动条。如下所示:
<UserControl x:Class="AutomaticContourEvaluation_WURO_WPF.Views.TwoDImageControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:AutomaticContourEvaluation_WURO_WPF.Views"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Source="{Binding Path=CurrentSlice}" x:Name="sliceDisplayControl" />
<ScrollBar Grid.Column="1" Minimum="0" Maximum="{Binding Path=NumberOfSlices, FallbackValue=10}" Value="{Binding Path=SliceIndex, Mode=TwoWay, FallbackValue=0}" x:Name="sliceSelectionScrollBar" Margin="0,0,0,0"/>
</Grid>
相应的ViewModel设计为:
internal class TwoDSliceViewModel : INotifyPropertyChanged
{
private List<BitmapImage> _imageSlices;
private int _sliceIndex = 0;
public BitmapImage CurrentSlice { get { return _imageSlices[SliceIndex]; } }
public int NumberOfSlices { get { return _imageSlices.Count; } }
public int SliceIndex
{
get
{
return _sliceIndex;
}
set
{
if (_sliceIndex != value)
{
_sliceIndex = value;
NotifyPropertyChanged("SliceIndex");
NotifyPropertyChanged("CurrentSlice");
}
}
}
public TwoDSliceViewModel()
{
BitmapImage demoImage = new BitmapImage();
demoImage.UriSource = new Uri("2Ddemo.jpg", UriKind.Relative);
demoImage.CacheOption = BitmapCacheOption.OnLoad;
if (_imageSlices == null)
{
_imageSlices = new List<BitmapImage>();
}
_imageSlices.Add(demoImage);
}
public TwoDSliceViewModel(List<BitmapImage> imageSlices)
{
_imageSlices = imageSlices;
}
public void updateImageSlices(List<BitmapImage> images)
{
_imageSlices = images;
_sliceIndex = 0;
NotifyPropertyChanged("CurrentSlice");
NotifyPropertyChanged("NumberOfSlices");
NotifyPropertyChanged("SliceIndex");
}
#region INotifyPropertyChanged Methods
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
在我的MainWindowViewModel中,有TwoDSliceViewModel的一个实例:
internal class MainWindowViewModel:INotifyPropertyChanged
{
...
private TwoDSliceViewModel _twoDSliceViewModel = new TwoDSliceViewModel();
public TwoDSliceViewModel TwoDModuleViewModel { get { return _twoDSliceViewModel; } }
...
}
为了绑定UserControl(TwoDSliceControl)工作,我在MainWindow.xaml中设置了UserControl的DataContext,如下所示:
<Window ...>
<Window.DataContext>
<viewModel:MainWindowViewModel />
</Window.DataContext>
...
<control:TwoDImageControl x:Name="twoDSliceImageControl" DataContext="{Binding Path=TwoDModuleViewModel}" />
...
</Window>
我的UserControl(TwoDSliceControl)是特定于ViewModel的,因此我选择这种方式,而不是使用“依赖项属性”。但是绑定失败。您可以在我的代码中看到,我在实例化TwoDSliceViewModel时创建了一些演示数据,但是这些虚拟数据没有显示。
我使用断点来发现成功初始化MainWindow之后,TwoDSliceControl的DataContext设置正确。但是TwoDSliceControl中ImageControl的Source属性和TwoDSliceControl中ScrollBar的MaxValue属性为空。
我的感觉是,这行XAML代码:
<control:TwoDImageControl x:Name="twoDSliceImageControl" DataContext="{Binding Path=TwoDModuleViewModel}" />
实际上首先要初始化twoDSliceImageControl,然后再设置twoDSliceImageControl.DataContext属性。初始化后,twoDSliceImageControl.DataContext为null,因此twoDSliceImageControl中的绑定失败。尽管初始化后,twoDSliceImageControl.DataContext设置正确,但UserControl中的绑定没有刷新,并且它们仍然为null。
有解决此问题的解决方法吗?已经停留了一段时间,没有找到合适的解决方案。谢谢你们!
谢谢大家。我终于找到了。这些代码行似乎存在一些问题:
BitmapImage demoImage = new BitmapImage();
demoImage.UriSource = new Uri("2Ddemo.jpg", UriKind.Relative);
demoImage.CacheOption = BitmapCacheOption.OnLoad;
相反,我需要使用BitmapImage.BeginInit()和BitmapImage.EndInit()来包装BitmapImage的创建。
因此,工作代码为:
BitmapImage demoImage = new BitmapImage();
demoImage.BeginInit();
demoImage.UriSource = new Uri("2Ddemo.jpg", UriKind.Relative);
demoImage.CacheOption = BitmapCacheOption.OnLoad;
demoImage.EndInit();
绑定实际上没有任何问题!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句