查看模型图像属性?
我有一个绑定到 TreeView 的视图模型列表,但是这些视图模型代表一个“文件系统”,例如带有“文件”和“文件夹”的数据结构。因此,在我的树视图视图的项目模板中,我有一个应该代表文件夹或文件的图像。
这是我的 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>
所以基本上我想知道如何将图像对象的源绑定到我的视图模型。
谢谢, 亚历克斯.
I have a list of view models which I am binding to a TreeView, however these view models are representing a 'file system' like data structure with 'files' and 'folders'. So in the item template on my view for the tree view I have an image which should represent either a folder or a file.
Here is My 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>
So basically I want to know how to bind the source of the image object to my view models.
Thanks,
Alex.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为最好的方法是使用这样的转换器:
然后声明一个名为 NodeType 的枚举:
在您的视图模型中,您声明一个名为 NodeType 的枚举类型的 INotifyPropertyChanged 属性。
然后在您的 XAML 中,您可以像这样声明转换器资源:
最后,您可以像这样绑定您的属性:
这样您就不需要处理 BitmapImage 声明并在视图模型中加载,并且仍然可以使其完全可绑定。
I think that the best way to do this is using a converter like this:
Then declare an enumeration called NodeType:
In your view model you declare an INotifyPropertyChanged property called NodeType of the enumeration type.
Then in your XAML you declare the converter resource like this:
Finally you bind your property like this:
This way you do not need to deal with BitmapImage declarations and loading in your view model and you can still make it fully bindable.
在您的视图模型中添加新属性 Icon,即将
DataTemplate 中的图像源更改为: Source="{Binding Path=Icon}"
并相应地在视图模型中加载图标,即对于文件夹视图模型,使用如下所示:
In your view models add new property Icon, i.e.
change Image's source in DataTemplate to: Source="{Binding Path=Icon}"
and load your icons in view model accordingly, i.e. for folder view model use something like this:
我发现这个解决方案非常有趣。在研究过程中,我对原始代码进行了一些适度的改进,我提交这些改进是希望其他人可以受益。
I found this solution very interesting. In studying it, I came up with a few modest improvements to the original code, which I submit in hopes someone else may benefit.