简体   繁体   English

WPF如何将图像作为ItemsSource的属性绑定到DataGrid列?

[英]WPF How to Bind an Image to a DataGrid column as a property of the ItemsSource?

I'm creating a WPF app using the MVVM design pattern. 我正在使用MVVM设计模式创建WPF应用。

I have a DataGrid with 4 columns and I want the last column to be a column of Images. 我有一个包含4列的DataGrid,我希望最后一列是Images的一列。 The other 3 text columns are bound to the properties of an AttributeNode contained in the list Attributes , which is used as the ItemsSource . 其他3个文本列绑定到AttributeNode列表中包含的Attributes ,该Attributes用作ItemsSource

XAML XAML

<UserControl.DataContext>
    <viewModel:AttributeGridViewModel />
</UserControl.DataContext>     

...

<DataGrid Name="AttributeGrid" HorizontalAlignment="Stretch" 
                VerticalAlignment="Stretch" ItemsSource="{Binding Attributes}" 
                AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Parameter" Binding="{Binding Parameter}"
                            MinWidth="30" Width="225" FontSize="15"/>
        <DataGridTextColumn Header="Value" Binding="{Binding Value}"
                            MinWidth="30" Width="*" FontSize="15"/>
        <DataGridTextColumn Header="IP Address" Binding="{Binding IpAddress}"
                            MinWidth="10" Width="120" FontSize="15"/>
        <DataGridTemplateColumn Header="Conflict" Width="50">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <!-- area of confusion -->
                    <Image Source="{Binding ???}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
   </DataGrid.Columns>
</DataGrid>

AttributeGridViewModel & AttributeNode AttributeGridViewModel和AttributeNode

In my AttributeGridViewModel I set the Attributes using a line like this: 在我的AttributeGridViewModel我使用如下行来设置属性:

Attributes = ConfigUtility.GetAttributeNodes(value);

Below is part of the AttributeNode class : 以下是AttributeNode类的一部分:

public class AttributeNode
{
    public string Parameter { get; set; }
    public string Value { get; set; }
    public string IpAddress { get; set; }
    public string ImageSource {get; set; }
    public char Type { get; set; }

    ...
}

You can see the properties here and how I attach the Parameter, Value, and IpAddress properties to their own columns. 您可以在此处看到这些属性,以及如何将Parameter,Value和IpAddress属性附加到它们自己的列。

But if I try to do this with the Image Source binding then I get this error : 但是,如果我尝试使用Image Source绑定执行此操作,则会收到此错误:

在此处输入图片说明

Question

I'm not sure yet if I can bind to a string that represents an Image path but I guess more importantly is how can I access and data bind to the property ImageSource for an item of type AttributeNode in the the ItemsSource Attributes (a list of Attributes )for the DataGrid? 我现在还无法确定,如果我可以绑定到表示影像路径,但我想更重要的是一个字符串是如何访问和数据绑定的属性ImageSource一个类型的项目AttributeNode中的的ItemsSource Attributes (名单DataGrid的Attributes )? My implementation works for the other 3 columns and updates correctly based on changes in my ViewModel. 我的实现适用于其他3列,并根据ViewModel中的更改正确更新。 How do I access the property like I did previously? 我如何像以前一样访问酒店?

For anybody wondering how I resolved this issue... 对于任何想知道我如何解决此问题的人...

It appears it is a Resharper issue and creating a new Attribute like this 看来这是一个Resharper问题,并像这样创建新的Attribute

new Attribute("","","","../Icons/checkmark.png")

works even though Resharper claims it can't find the ImageSource property. 即使Resharper声称找不到ImageSource属性,它仍然可以工作。 The DataContext is set correctly during run time. 在运行时正确设置了DataContext。 The Images show up in the column now. 图像现在显示在列中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM