在我的View中,这是一个 UserControl 我有一个ListView,它有许多GridViewColumns,我想知道是否可以将第一个 GridViewColumns绑定到我的ViewModel中的ImageTypes Get / Set属性和ViewModel中另一个WorkflowData GetSet属性的其他两列?
原因是我的第一列来自图像列表,其他列来自存储在我的数据库中的数据。
目前,我似乎无法找到正确的绑定,以使图标显示在列表中,其他数据来自模型(通过ViewModel)。我是否需要在Image的绑定中设置“RelativeSource”?
从我的例子中,你会看到我现在通过将uri作为字符串传递来简单地尝试显示一个图像。
任何帮助将不胜感激。 感谢
XAML查看摘要
<UserControl x:Class="Project.View.WorkflowStatus"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mvvmtk="clr-namespace:MVVMToolkit"
Height="Auto"
xmlns:local="clr-namespace:Project.ViewModel">
<UserControl.DataContext>
<local:WorkflowStatusViewModel />
</UserControl.DataContext>
<ListView Grid.Column="0" Grid.Row="1"
ItemsSource="{Binding Path=WorkflowData.WFTasks, Mode=TwoWay}"
SelectedValue="{Binding Workflow.WFTask}"
HorizontalContentAlignment="Stretch">
<ListView.View>
<GridView>
<GridViewColumn Header="" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Image Source="{Binding ImageTypes, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type local:WorkflowStatusViewModel}}}"
Stretch="None"></Image>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Field1"
Width="50"
DisplayMemberBinding="{Binding Path=Field1Value1}"></GridViewColumn>
<GridViewColumn Header="Field2"
Width="50"
DisplayMemberBinding="{Binding Path=Field1Value2}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</UserControl>
查看模型代码段
/// <summary>
// This is the main class which links the Model and View together.
/// </summary>
public class WorkflowStatusViewModel
{
/// <summary>
// Create a public instance of the workflow Model
/// </summary>
public WorkflowModel WorkflowData { get; set; }
/// <summary>
/// Class Constructor
/// </summary>
public WorkflowStatusViewModel()
{
InitialiseData();
}
///MatterManagerView;component/Assets/Task.png
private string _ImageTypes;
public string ImageTypes
{
get { return "Project;component/Assets/Task.png"; }
set { _ImageTypes = value; }
}
/// <summary>
// Private function that create all the neccessary objects
/// </summary>
private void InitialiseData()
{
try
{
// Create a new instance of the Workflow Model
WorkflowData = new WorkflowModel();
}
// Write Errors captured to Aderant Logging object
catch (Exception ex)
{
}
}
编辑:根据Marcel的建议,我尝试使用RelativeSource映射图像
答案 0 :(得分:1)
是的,可以通过RelativeSource
完成listview datacontext指向WorkflowData.WFTasks,但我希望viewmodel本身绑定到放置listview的页面。 否则,您应该更改AncestorType
完成测试项目后,您的图像列绑定为:
Source="{Binding Path=DataContext.ImageTypes,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type UserControl}}}"