【发布时间】:2011-06-05 05:31:17
【问题描述】:
在我的视图中,这是一个 UserControl 我有一个 ListView,它有许多 GridViewColumns,我想知道是否可以将 first GridViewColumns 绑定到我的 ViewModel 中的 ImageTypes Get/Set 属性以及 ViewModel 中另一个 WorkflowData GetSet 属性的其他两列?
这样做的原因是我的第一列将来自图像列表,而其他列来自存储在我的数据库中的数据。
目前我似乎找不到正确的绑定来让图标出现在列表中,而其他数据来自模型(通过 ViewModel)。我需要在图像的绑定中设置“RelativeSource”吗?
从我的示例中,您会看到我现在很简单,通过将 uri 作为字符串传递来显示一个图像。
任何帮助将不胜感激。 谢谢
XAML 视图 sn-p
<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 映射图像
【问题讨论】:
标签: c# xaml data-binding listview mvvm