我有一个绑定(bind)到 TreeView 的 View 模型列表,但是这些 View 模型表示一个“文件系统”,例如带有"file"和“文件夹”的数据结构。因此,在我的 TreeView View 的项目模板中,我有一个应该代表文件夹或文件的图像。
这是我的 XAML:
<StackPanel Orientation="Horizontal">
<!-- Folder Icon -->
<Image Width="15" Height="15" Stretch="Fill" Source="\Resources\Folder.png"></Image>
<Grid>
<!-- Folder Name -->
<Label Content="{Binding Path=FolderName}">
<!-- Force Selection on Right Click -->
<ACB:CommandBehaviourCollection.Behaviours>
<ACB:BehaviourBinding Event="PreviewMouseRightButtonDown" Command="{Binding Path=MainModel.SelectTreeViewItem}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}}"></ACB:BehaviourBinding>
</ACB:CommandBehaviourCollection.Behaviours>
</Label>
<!-- Folder Name Editor -->
<StackPanel Name="FolderEditor" Orientation="Horizontal" Visibility="Collapsed">
<TextBox Text="{Binding Path=FolderName}" Width="130"></TextBox>
<Button Content="Ok" Command="{Binding Path=RenameFolder}" CommandParameter="{Binding ElementName=FolderEditor}"></Button>
</StackPanel>
</Grid>
</StackPanel>
所以基本上我想知道如何将图像对象的源绑定(bind)到我的 View 模型。
谢谢,
亚历克斯。
最佳答案
我认为最好的方法是使用这样的转换器:
public class EnumToResource : IValueConverter
{
public List<object> EnumMapping { get; set; }
public EnumToResource()
{
EnumMapping = new List<object>();
}
public virtual object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int adjustment = 0;
if (parameter != null && !Int32.TryParse(parameter.ToString(), out adjustment))
{
adjustment = 0;
}
if (value == null) return this.EnumMapping.ElementAtOrDefault(0);
else if (value is bool)
return this.EnumMapping.ElementAtOrDefault(System.Convert.ToByte(value) + adjustment);
else if (value is byte)
return this.EnumMapping.ElementAtOrDefault(System.Convert.ToByte(value) + adjustment);
else if (value is short)
return this.EnumMapping.ElementAtOrDefault(System.Convert.ToInt16(value) + adjustment);
else if (value is int)
return this.EnumMapping.ElementAtOrDefault(System.Convert.ToInt32(value) + adjustment);
else if (value is long)
return this.EnumMapping.ElementAtOrDefault(System.Convert.ToInt32(value) + adjustment);
else if (value is Enum)
return this.EnumMapping.ElementAtOrDefault(System.Convert.ToInt32(value) + adjustment);
return this.EnumMapping.ElementAtOrDefault(0);
}
public virtual object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
然后声明一个名为 NodeType 的枚举:
enum NodeType
{
Folder,
File,
}
在您的 View 模型中,您声明了一个名为 NodeType 枚举类型的 INotifyPropertyChanged 属性。
然后在您的 XAML 中声明转换器资源,如下所示:
<Converters:EnumToResource x:Key="IconConverter">
<Converters:EnumToResource.EnumMapping>
<BitmapImage UriSource="\Resources\Folder.png"/>
<BitmapImage UriSource="\Resources\File.png"/>
</Converters:EnumToResource.EnumMapping>
</Converters:EnumToResource>
最后你像这样绑定(bind)你的属性:
<Image Source="{Binding Path=NodeType, Converter={StaticResource ResourceKey=IconConverter}}"/>
这样您就不需要处理 BitmapImage 声明和加载到您的 View 模型中,您仍然可以使其完全可绑定(bind)。
关于.net - 查看模型图像属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7870855/