c# - 从 ListView 显示所选图像的新窗口无权访问图像路径
问题描述
我有一个带有 ListView 的应用程序,我通过单击添加图像按钮将图像添加到该列表中,我还有一个用于从该列表中删除图像的按钮。我为此使用 SelectedImage 对象,一切正常。但我想要一个按钮来显示选定的图像,这就是我这样做的方式:
private ObservableCollection<Image> imageList = new ObservableCollection<Image>();
private Image selectedImage = new Image();
public MainViewModel()
{
AddImagesCommand = new RelayCommand(AddImagesMethod);
RemoveImagesCommand = new RelayCommand(RemoveImagesMethod);
ShowImageCommand = new RelayCommand(ShowImageMethod);
SelectedImageCommand = new RelayCommand(SelectedImageMethod);
}
public ICommand AddImagesCommand { get; set; }
public ICommand ShowImageCommand { get; set; }
public ICommand RemoveImagesCommand { get; set; }
public ICommand SelectedImageCommand { get; set; }
public ObservableCollection<Image> ImageList
{
get
{
return imageList;
}
}
public Image SelectedImage
{
get
{
return selectedImage;
}
set
{
selectedImage = value;
}
}
public void SelectedImageMethod()
{
Image selected = SelectedImage;
RaisePropertyChanged("SelectedImage");
}
public void AddImagesMethod()
{
...
blablabla
....
imageList.Add(new Image
{
Name = _name,
SafeName = safeImageName,
Weight = sLen,
CheckboxDefault = false
}) ;
this.RaisePropertyChanged(() => this.ImageList);
}
public void RemoveImagesMethod()
{
imageList.Remove(SelectedImage);
RaisePropertyChanged("RemoveImagesCommand");
}
public void ShowImageMethod()
{
//string V = @"pack://application:,,,/inred;component/Sources/test.jpg";
//string V = @"pack://application:,,,/inred;component/Sources/" + SelectedImage.SafeName;
ImageWindow win2 = new ImageWindow(SelectedImage.Name);
win2.Show();
RaisePropertyChanged("ShowImageCommand");
}
问题是 SelectedImage.Name 和 SelectedImage.SafeName 是空的,我不明白为什么相同的 SelectedImage 用于 RemoveImageCommand 并且工作正常。带有 test.jpg 的字符串 V 有效,但第二个 V 无效。请给我一些指示,我缺少什么。
更新: 这是 MainWindow 的 XAML:
<Grid DataContext="{Binding MainViewModel, Source={StaticResource Locator}}" >
<ListView x:Name="Images" HorizontalAlignment="Left" Height="100" Margin="20,148,0,0" VerticalAlignment="Top" Width="581" ItemsSource="{Binding ImageList}" SelectedItem="{Binding SelectedImage}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding SafeName}" />
<GridViewColumn Header="Description" Width="120">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox HorizontalAlignment="Left" Height="23" VerticalAlignment="Top" Width="200" Text="{Binding Description}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Weight" Width="75" DisplayMemberBinding="{Binding Weight}" />
<GridViewColumn Header = "Actions" Width = "150">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Grid>
<Button Content="Show"
CommandParameter="{Binding}"
Command="{Binding DataContext.ShowImageCommand,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type local:MainWindow}}}" />
</Grid>
<Grid>
<Button Content="Annotate"
CommandParameter="{Binding}"
Command="{Binding DataContext.AnnotateImageCommand,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type local:MainWindow}}}" />
</Grid>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header = "Default" Width = "50">
<GridViewColumn.CellTemplate>
<DataTemplate>
<RadioButton GroupName="allFiles" IsChecked="{Binding CheckboxDefault}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Button Content="Add Image" HorizontalAlignment="Left" Margin="645,148,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.282,-0.25" Command="{Binding AddImagesCommand}"/>
<Button Content="Remove Image" HorizontalAlignment="Left" Margin="645,226,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.282,-0.25" Command="{Binding RemoveImagesCommand}"/>
</Grid>
这是ImageWindow:
public partial class ImageWindow : Window
{
public ImageWindow(string imagePath)
{
InitializeComponent();
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(imagePath);
bitmap.EndInit();
ImageViewer1.Source = bitmap;
}
}
解决方案
您确定单击按钮时选择了一个项目吗?此外,由于您将 绑定CommandParameter
到 中的当前Image
,ListView
您应该在命令中使用此参数:
ShowImageCommand = new RelayCommand<Image>(ShowImageMethod);
...
public void ShowImageMethod(Image image)
{
ImageWindow win2 = new ImageWindow(image.Name);
win2.Show();
}
推荐阅读
- php - PHP 不会显示数据并且不显示错误
- javascript - 使用动态对象名称更新数据库 (Node.js)
- ruby-on-rails - 查询空的多态关联
- vba - 筛选包含条件的 PowerPivot 字段
- sql-server - 如何更改 Microsoft SQL Server Management Studio 2017 中的文本大小?
- json - 发布到 URL 时如何格式化 Json 日期?
- sql - Clickhouse,列值到数组
- python - Django:属性错误
- sql - 如何为sql server导入jdbc驱动
- wordpress - 在所有其他通知之后在结帐时添加 WC 通知