[英]Using Image tag in xaml with what type of property
I want to use an image in xaml with a <Image></Image>
tag (Inside of a ListBox).我想在 xaml 中使用带有
<Image></Image>
标记的<Image></Image>
(在列表框内)。
<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.并在 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.现在,
ClassImage
属性只是一个字符串属性(我在某处读到它应该工作)但图像没有显示,但我在ListOfModels
视图中使用的其余数据工作正常,我可以看到三个对象(空)在列表中的视图中。 The images in my Image folder are of type Resource and on copy if newer.我的 Image 文件夹中的图像属于 Resource 类型,如果更新则为副本。
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?我的问题是我猜,将 Image.Source 属性绑定到什么类型的“属性”的最佳方式是什么,它是真正使用字符串的最佳方式吗? 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.您的 ChooseNewClassViewModel 方法(构造函数?)不执行任何操作,因为它首先创建一个模型列表,然后立即迭代该集合,但该集合是空的,因为它尚未填充。
Perhaps you want your ClassImage
property to look like this:也许您希望
ClassImage
属性如下所示:
public string ClassImage
{
get { return AppDomain.CurrentDomain.BaseDirectory...; }
}
The Problem was that my xaml could not find the binding to my Model.问题是我的 xaml 找不到与我的模型的绑定。 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.
我修复它的快速方法只是我在我的视图模型中创建了一个新的字符串道具,将 xaml 绑定到那个,并用它来显示图像。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.