将 Gridview 中选择的项目的值传递给不同用户控件的 ViewModel [英] Passing the value of an item selected in a Gridview to the ViewModel of a different Usercontrol

查看:11
本文介绍了将 Gridview 中选择的项目的值传递给不同用户控件的 ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

研究了三天的解决方案后,我终于放弃了.现在我需要你的帮助来解决这个问题.

After researching a solution to this for three days, finally I gave up. Now I need your help to solve this.

我在 Usercontrol 中有一个 GridView(比如 WLMSLogs.xaml),我的 GridView ItemSource 绑定到 ViewModel (WMLSLogsViewModel.cs) 中的一个列表

I've a GridView in a Usercontrol (Lets say WLMSLogs.xaml) and My GridView ItemSource is binded to a List from the ViewModel (WMLSLogsViewModel.cs)

假设列表有 4 个项目(事件 ID、名称、请求和响应).请求和响应都是 XML 字符串.

Lets say the List has 4 items (EventID, Name, Request and Response). BothRequest and Responses are XML Strings.

GridView 需要在不同的标签项下显示 RowDetailsTemplate 中的一些 List 项.所以我在各自的 TabItems 下显示 Request 和 Response.

GridView needs to display some of the List items in RowDetailsTemplate under different tab items. So I'm displaying Request and Response under respective TabItems.

<GridView x:Name="WMLSLogGrid" ItemsSource="{Binding WMLSLogModelList}">
<GridView.Columns>      
    <GridViewDataColumn DataMemberBinding="{Binding ID}" Header="ID"/>
    <GridViewDataColumn DataMemberBinding="{Binding UserName}" Header="UserName"/>     
</GridView.Columns>
<GridView.RowDetailsTemplate>
    <DataTemplate >                                                 
            <TabControl>
                <TabItem Header="Request Xml">
                    <TextBlock text="{Binding Request}"/>
                </TabItem>
            <TabItem Header="Response Xml">
                <TextBlock text="{Binding Response}"/>
            </TabItem>
                <TabItem Header="EventLogs" Tag="{Binding .}">
                    <views:LogEvents  />
                </TabItem>
            </TabControl>
        </Grid>
    </DataTemplate>
</GridView.RowDetailsTemplate>

WMLSLogModelList 是 WMLSLogsViewModel.cs 中的 Observable 集合

WMLSLogModelList is a Observable collection in WMLSLogsViewModel.cs

到目前为止一切正常,Grid 正在按预期显示数据.

So far everything works fine and Grid is displaying data as expected.

现在当用户展开任何行时,他可以看到两个带有请求和响应的 TabItem.这里我需要在Request和Response选项卡之外再添加一个TabItem(LogEvents).

Now When User expands any row, he can see two TabItems with request and response. Here I need to add one more TabItem (LogEvents) besides Request and Response tabs.

此 LogEvents 选项卡将再显示一个 GridView(因此我在选项卡中添加了一个新视图 ).棘手的部分来了.

This LogEvents tab is going have one more GridView to display (so I added a new View <views:LogEvents /> in the tab). Here comes the tricky part.

这个LogEvents GridView需要根据对应的Selecteditem(也就是EventId)获取数据,并将这个EventId传递给不同的ViewModel (LogEventViewModel.cs) 并将数据动态绑定到内部 GridView.所有这些都必须在我展开 RowDetails 部分或选择 Tab LogEvents 时发生.

This LogEvents GridView needs to get the data based on the corresponding Selecteditem (which is EventId), and pass this EventId to a different ViewModel (LogEventViewModel.cs) and binds the data to the Inner GridView dynamically. All this has to happen either as I expand the RowDetails section or if I select the Tab LogEvents.

这两个 Grid 的数据项之间没有任何关系,除了获取主 GridView 的 Selected EventId 并将其传递给不同的 ViewModel 再传递给 Domain 服务以获取内部 GridView 数据.

There is no relation between the data items of these two Grids, except getting the Selected EventId of the main GridView and passing this to a different ViewModel then to Domain service to get the inner GridView Data.

正如我所提到的,我为 LogEvents 创建了一个新的 View UserControl,将其放置在主 GridView 的行详细信息模板内的 new TabItem(EventLogs) 下.

As I mentioned I created a new View UserControl for LogEvents, Placed it under new TabItem(EventLogs) inside row details template of main GridView.

LogEvent UserControl 包含一个绑定到 LogEventsViewModel 的网格视图,以根据选定行的 EventId 获取集合.

LogEvent UserControl contains a Grid View binded to LogEventsViewModel to get the Collection based on Selected row EventId.

如何将 Selected EventId 分配给新的 ViewModel 并动态获取数据?

How do I assign the Selected EventId to a new ViewModel and Get the data dynamically?

一种方式:正如我向您展示的那样,我通过将 LogEvents 放在旁边的 TabItem 来调用它.每当我展开任何行时,它都会初始化 LogEvents 页面,在此期间我尝试将 Datacontext 绑定到 LogEventsViewModel.但我无法动态获取 Selected 行 EventId.如果我得到了,那么我可以轻松地将它传递给 LogEventsViewModel 构造函数.

One Way: As I showed you, I called LogEvents by placing it in side TabItem. whenever I expanded any row, Then It is Initializing the LogEvents page, During that I tried to bind the Datacontext to LogEventsViewModel. But I'm unable to get the Seleted row EventId dynamically. If I get that then I can easily pass it to the LogEventsViewModel constructor.

       var _viewModel = new LogEventsViewModel(EventId);
       this.DataContext = _viewModel;            
       InitializeComponent();

其他方式:将选定的 EventId 直接从 xaml 视图绑定传递到该页面初始化,然后传递到 LogEventsViewModel类似这样的

Other way: Pass the selected EventId directly from xaml View binding to that page initialization and then to LogEventsViewModel Something like this

<views:LogEvents  something="{Binding EventId}"/>

有没有其他方法可以做到这一点?

Is there any other way to do this?

日志事件.xaml

<UserControl.Resources>
    <viewModel:LogEventsViewModel x:Key="ViewModel"/>
</UserControl.Resources>
<Grid DataContext="{Binding Source={StaticResource ViewModel}}">
    <GridView x:Name="LogEventsGrid" ItemsSource="{Binding View}"            
                 <GridView.Columns>
            <telerik:GridViewToggleRowDetailsColumn />              
            <telerik:GridViewDataColumn DataMemberBinding="{Binding EventId}" Header="LogEventId"/>  
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Exception}" Header="Exception" />
        </GridView.Columns>
    </telerik:RadGridView>
</Grid>

LogEvents.xaml.cs

LogEvents.xaml.cs

public int EventId
    {
        get { return (int)GetValue(EventIdProperty); }
        set { SetValue(EventIdProperty, value); }
    }
    public static readonly DependencyProperty EventIdProperty =
    DependencyProperty.Register("EventId", typeof(int), typeof(ApplicationLog),
    new PropertyMetadata(0, new PropertyChangedCallback(OnEventIdChanged)));

    private static void OnEventIdChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {            
        int LogEventId1 = (int)e.NewValue;
        // Need to assign propery in LogEventsViewModel 
    }

LogEventsViewModel.cs

LogEventsViewModel.cs

WMLCDomainContext _Context = new WMLCDomainContext();
    private QueryableDomainServiceCollectionView<LogEvents> view;
    public int _eventid;       

    public ApplicationLogsViewModel()
    {
        EntityQuery<LogEvents> getLogEventsQuery = _Context.GetApplicationLogListQuery(EventId);
        this.view = new QueryableDomainServiceCollectionView<ApplicationLog>(_Context, getLogEventsQuery );                        
    }

    public IEnumerable View
    {get {return this.view;}}

    public int EventId
    {
        get{return this._eventid;}
        set{_eventid = value;}
    }

推荐答案

我会在您的 LogEventsViewModel 上创建一个依赖属性,然后在您的 LogEvents 视图上设置一个绑定,如下所示:

I would create a dependency property on your LogEventsViewModel and then set up a binding on your LogEvents view, something like this:

<views:LogEvents EventId="{Binding EventId}" />

然后在 LogEvents.xaml.cs 中你可以创建你的依赖属性:

Then in LogEvents.xaml.cs you could create your dependency property:

    private LogEvents_ViewModel _viewModel
    {
        get { return this.DataContext as LogEvents_ViewModel; }
    }

    public string EventId
    {
        get { return (string)GetValue(EventIdProperty); }
        set { SetValue(EventIdProperty, value); }
    }
    public static readonly DependencyProperty EventIdProperty =
        DependencyProperty.Register("EventId", typeof(string), typeof(LogEvents),
        new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnEventIdChanged)));

    private static void OnEventIdChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((LogEvents)d).OnTrackerInstanceChanged(e);
    }

    protected virtual void OnEventIdChanged(DependencyPropertyChangedEventArgs e)
    {
        this._viewModel.EventId = e.NewValue;
    }

这篇关于将 Gridview 中选择的项目的值传递给不同用户控件的 ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆