Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Image in UserControl not visible

$
0
0

Hello, I am using MVVM Light and i have the following problem:

I am having a window which has an ItemsControl in the xaml, which looks like this:

<Window x:Class="YASU_MVVM.Views.ScreenshotHistoryWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:YASU_MVVM.Views"
        mc:Ignorable="d"
        Title="ScreenshotHistoryWindow" Height="600" Width="450"
        Background="{DynamicResource WindowBackgroundBrush}"
        DataContext="{Binding ScreenshotHistoryWindowViewModel, Source={StaticResource Locator}}"><ScrollViewer VerticalScrollBarVisibility="Visible"><ItemsControl ItemsSource="{Binding NewScreenshotCollection}"/></ScrollViewer></Window>

This ItemsControl referes to a property, NewScreenshotCollection, which essentially looks like this:

        private ObservableCollection<ImageHistoryEntryControl> _newScreenshotCollection;
        public ObservableCollection<ImageHistoryEntryControl> NewScreenshotCollection
        {
            get { return _newScreenshotCollection; }
            set { Set(ref _newScreenshotCollection, value); }
        }
        #endregion

(Its called NewScreenshotCollection because ScreenshotCollection is the same as above just with a collection of Image (System.Windows.Controls.Image) instead of my usercontrol; Which works fine, the problem is only with the UC).

Now, the UserControl markup looks like this:

<UserControl x:Class="YASU_MVVM.UserControl.ImageHistoryEntryControl"
             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:YASU_MVVM.UserControl"
             mc:Ignorable="d"
             d:DesignHeight="240" d:DesignWidth="386"
             Background="Black"><Border BorderBrush="White" BorderThickness="2"><Grid><Grid.RowDefinitions><RowDefinition Height="216" /><RowDefinition Height="20" /></Grid.RowDefinitions><Image Grid.Row="0" Source="{Binding PrevImage}" /><!--<Image Grid.Row="0"><Image.Source><BitmapImage UriSource="{Binding BImageUri}" /></Image.Source></Image>--><Label Grid.Row="1" Content="{Binding PrevImageText}" /></Grid></Border></UserControl>


(the part that i commented out is something i found in a thread here in the forums, where someone apparently got it to work by using a BitmapImage UriSource as Image.Source - doesnt work for me, it just throws a runtime exception of the xaml parser).

Now, when Initializing, i do this:

        public ScreenshotHistoryWindowViewModel()
        {
            _userConfiguration = SimpleIoc.Default.GetInstance<IUserConfiguration>();
            ScreenshotCollection = new ObservableCollection<Image>();
            NewScreenshotCollection = new ObservableCollection<ImageHistoryEntryControl>();
            FillScreenshotCollection();
        }

FillScreenshotCollection() is this:

        private void FillScreenshotCollection()
        {
            foreach (string s in Directory.GetFiles(_userConfiguration.GetScreenshotPath())
                    .Where(f => f.EndsWith("jpg") || f.EndsWith("jpeg") || f.EndsWith("png") || f.EndsWith("bmp")))
            {
                ImageHistoryEntryControlViewModel model = new ImageHistoryEntryControlViewModel(s, "kein text");
                ImageHistoryEntryControl iec = new ImageHistoryEntryControl { DataContext = model };
                _newScreenshotCollection.Add(iec);
            }
        }

The constructor for ImageHistoryEntryControlViewModel looks like this:

public ImageHistoryEntryControlViewModel(string imagePath, string text)
        {
            PrevImageText = text;

            using (FileStream fs = new FileStream(imagePath, FileMode.Open))
            {
                BitmapImage tmp = new BitmapImage();
                tmp.BeginInit();
                tmp.CacheOption = BitmapCacheOption.OnLoad;
                tmp.StreamSource = fs;
                tmp.EndInit();
                tmp.Freeze();

                PrevImage = new Image { Source = tmp };

            }
        }

(I also once tried to just hand over the Image, then the ImageSource on another try - nothing worked).

I really dont know what else to try - does anybody have an idea why the image doesnt display or how to get it to work? I dont think that I can add the images themselfes to the project since they are generated by the user and placed in a folder.
Also if not using the UserControl it works fine, by just using Image - but I want it to work like this (its a project i do to learn some MVVM Light and other things about WPF thats why i strictly want to go with UserControls).


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>