【问题标题】:Bind a ListView ItemSource to two different GetSet properties in the View (MVVM)将 ListView ItemSsource 绑定到 View (MVVM) 中的两个不同 GetSet 属性
【发布时间】: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


    【解决方案1】:

    是的,可以通过RelativeSource完成

    列表视图数据上下文指向 WorkflowData.WFTasks,但我希望视图模型本身绑定到放置列表视图的页面。 否则你应该改变 AncestorType

    做一个测试项目后,为你绑定的图片栏是:

    Source="{Binding Path=DataContext.ImageTypes, 
             RelativeSource={RelativeSource FindAncestor, 
             AncestorType={x:Type UserControl}}}"
    

    【讨论】:

    • 很抱歉造成混淆,但我已将此 sn-p 应用到我的图像源中,但仍然没有显示任何内容。我还在该属性上设置了一个断点,但它没有进入它。我在正确的地方应用它吗?我已将 x:Type 从 Page 更改为 UserControl。
    • 绑定可能很棘手,我从绑定到可观察集合的列表视图中复制出来,其中一个列中有一个组合框,它的 itemssource 到另一个属性。 stackoverflow 上还有其他示例。如果您发布完整的页面/用户控件 xaml 源,我可以看看吗?
    • 嗨,Marcel,我从我的 XAML 中添加了更多源代码。我也会继续关注 SO,看看我是否能找出我所缺少的东西。感谢您的帮助 +1
    • 嗨 Marc,我必须将 AncestorType 更改为 AncestorType={x:Type UserControl}}}" 才能正常工作。我会接受你的回答,因为它让我在那里,但如果你能改变最后一部分反映我为使其正常工作所做的工作,我将不胜感激。
    猜你喜欢
    • 2012-01-08
    • 2020-05-17
    • 2016-03-28
    • 2019-01-18
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 1970-01-01
    相关资源
    最近更新 更多