页面加载时MVVM加载模型数据 [英] MVVM load model data when the Page loads

查看:61
本文介绍了页面加载时MVVM加载模型数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过[可能]使用

By implementing MVVM [possibly] correctly using this blog post as an example, I managed to get my data showing on my DataGrid.

请参见我先前的历史记录.

唯一的是,当我在我当前通过按钮命令执行的ViewModel上执行 ListCampaignsCommand 时,DataGrid仅绑定到我的数据上:

The only thing is that the DataGrid only binds to my data when I execute ListCampaignsCommand on my ViewModel which I'm currently doing through a Button Command:

<!-- Pages\ManageCampaigns.xaml -->
<Button Name="btnRefresh" Grid.Column="0" Command="{Binding ListCampaignsCommand}"
    Style="{StaticResource TitleButtonLink}" Margin="20,4,0,0">Refresh List
</Button>

<DataGrid x:Name="campaignDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True"
            ItemsSource="{Binding Campaigns}" Grid.Row="1" 
            RowDetailsVisibilityMode="VisibleWhenSelected">
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="idColumn" Binding="{Binding Id}"
                            Header="#" Width="SizeToCells" />
        <DataGridTextColumn x:Name="clientColumn" Binding="{Binding Client}"
                            Header="Client" Width="SizeToHeader" />
        <DataGridTextColumn x:Name="dateSentColumn" Binding="{Binding DateSent}"
                            Header="Date" Width="SizeToCells" />
        <DataGridTextColumn x:Name="nameColumn" Binding="{Binding Name}" Header="Name" Width="SizeToCells"/>
        <DataGridTextColumn x:Name="emailAddressColumn" Binding="{Binding EmailAddress}" Header="Email Address" Width="SizeToCells"/>
    </DataGrid.Columns>
</DataGrid>

我的ViewModel(可能做的比预期更多...该示例未涉及使用MVVM与EF数据事物进行交互)获取数据:

My ViewModel (which probably does more than it should... The example didn't involve using MVVM to interact with EF data things) gets the data:

// CampaignViewModel
private readonly ObservableCollection<Campaign> _campaigns
    = new ObservableCollection<Campaign>();

public IEnumerable<Campaign> Campaigns
{
    get { return _campaigns; }
}

public ICommand ListCampaignsCommand
{
    get { return new DelegateCommand(ListCampaigns); }
}
public void ListCampaigns()
{
    using (ApplicationDbContext db = new ApplicationDbContext())
    {
        var Campaigns = db.Campaigns.Where(x => x.Deleted == false);
        foreach (var c in Campaigns)
        {
            AddToCampaigns(c);
        }
    }
}
private void AddToCampaigns(Campaign campaign)
{
    if (!_campaigns.Contains(campaign))
        _campaigns.Add(campaign);
}

当我尝试在< Page ...> 上添加 Loaded = {Binding ListCampaignsCommand} 时,我得到了一个非常隐秘的错误消息:

When I tried adding Loaded={Binding ListCampaignsCommand} on my <Page ...> I got a very cryptic error message:

抛出异常:PresentationFramework.dll中的'System.Windows.Markup.XamlParseException'

Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll

其他信息:在'System.Windows.Data.Binding'上提供的值引发了异常.行号"10"和行位置"7".

Additional information: 'Provide value on 'System.Windows.Data.Binding' threw an exception.' Line number '10' and line position '7'.

该行号和位置是 Loaded 调用所在的位置.

That line number and position is where the Loaded call is.

如何打开页面打开页面以加载数据?

How can I get the Page to load the data when it opens?

推荐答案

如何打开页面打开页面以加载数据?

How can I get the Page to load the data when it opens?

如何在视图模型的构造函数中调用 ListCampaigns()方法?

How about calling your ListCampaigns() method in the constructor of the view model?

public class CampaignViewModel
{
    public CampaignViewModel()
    {
        ListCampaigns();
    }

    //the rest of your code...
}

这篇关于页面加载时MVVM加载模型数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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