.net - 查看模型图像属性?

标签 .net wpf xaml mvvm .net-4.0

我有一个绑定(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/

相关文章:

c# - 当未选择行时,复选框列第一次不会更改其状态

C# .net framework- 边框仅在表单的一侧

c# - 如何在 WPF 应用程序中捕获命令行参数?

c# - WPF:如何使 RichTextBox 看起来像 TextBlock?

c# - 使用 MVVM 的 wpf 中的案例约定

silverlight - 在 MVVM Light 中将文本 block 绑定(bind)到文本框

c# - 如何使用 IoC 容器在不同的地方使用不同的实现

c# - Entity Framework 4 中的 Linq 查询。糟糕的性能

c# - 相对于中心而不是左上角缩放 WPF Canvas

c# - 在 WPF XAML 窗口上添加图标会导致错误/崩溃 VS2012