I want to use an image in xaml with a <Image></Image>
tag (Inside of a ListBox).
<Image Source="{Binding ClassImage}"
Width="64" Height="64"
HorizontalAlignment="Left" VerticalAlignment="Top"
Stretch="Fill"
Margin="0,0,0,10"
RenderOptions.BitmapScalingMode="Fant"/>
And using this constructor in the ViewModel.
public ChooseNewClassViewModel()
{
ListOfModels = new ObservableCollection<Model>();
foreach (var model in ListOfModels)
{
Model.ClassImage = AppDomain.CurrentDomain.BaseDirectory + @"Common\Assets\Images\Image\" + PlayerClass.Name + ".png";
}
}
And here is the Model property
public string ClassImage
{
get { return _classImage; }
set
{
_classImage = value;
OnPropertyChanged(nameof(ClassImage));
}
}
Now, The ClassImage
property is only a string property ( I read somewhere that it should work) but the images are not showing, but the rest of data I use in the view from ListOfModels
works fine, and i can see three objects (empty) in the view from the list. The images in my Image folder are of type Resource and on copy if newer.
My question is I guess, whats the best way of binding the Image.Source property to what type of "property", is it the best way really to use a string? Whats the best way of using images you store in your project?
Your ChooseNewClassViewModel method (constructor?) doesn't do anything, because it first creates a list of models, then immediately iterates over the collection, but the collection is empty, because it hasn't been populated.
Perhaps you want your ClassImage
property to look like this:
public string ClassImage
{
get { return AppDomain.CurrentDomain.BaseDirectory...; }
}
The Problem was that my xaml could not find the binding to my Model. The quick way i fixed it is just i created a new string prop in my viewmodel, bound the xaml to that one instead and used it to show the images.
public string ClassImage
{
get { return _classImage; }
set
{
_classImage = value;
OnPropertyChanged(nameof(ClassImage));
}
}
foreach (var PlayerClass in ListOfPlayerClasses)
{
ClassImage = @"/path/folders/" + PlayerClass.Name + ".png";
PlayerClass.ClassImage = ClassImage;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.