Silverlight子窗口中的绑定问题

时间:2011-02-15 12:01:10

标签: silverlight silverlight-4.0

我正在使用Silverlight子窗口,其中我放置了Silverlight网格。示例代码如下所示:

<controls:ChildWindow x:Class="Bixi.Atlas.Client.UI.OrganisationModule.Views.CostCenters.Lookup"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
           Width="400" Height="300" 
                       xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
             xmlns:viewmodel="clr-namespace:Bixi.Atlas.Client.UI.OrganisationModule.ViewModels.CostCenters"
           Title="Lookup">

    <Grid x:Name="LayoutRoot" Margin="2" DataContext="{Binding PersonLookupVM, Source={StaticResource PersonLookupViewModel}}">
        <sdk:DataGrid AutoGenerateColumns="False" Height="550" Width="750" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,0,0,0" Name="PersonDetailsGrid" 
                      ItemsSource="{Binding Path=GetPersonDetails, Mode=TwoWay}">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Width="100" IsReadOnly="True" Header="Person ID" Binding="{Binding PRS_PER_CODE, Mode=TwoWay}"></sdk:DataGridTextColumn>
                <sdk:DataGridTextColumn Width="100" Header="Name" Binding="{Binding PRS_PER_NAME, Mode=TwoWay}"></sdk:DataGridTextColumn>
                <sdk:DataGridTextColumn Width="100" Header="First name" Binding="{Binding PRS_PER_FIRSTNAME, Mode=TwoWay}"></sdk:DataGridTextColumn>
                <sdk:DataGridTextColumn Width="100" Header="Country" Binding="{Binding PRS_PER_COUNTRY, Mode=TwoWay}"></sdk:DataGridTextColumn>
            </sdk:DataGrid.Columns>
            <!--<i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}" CommandParameter="{Binding SelectedItems, ElementName=CostCentersGrid}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>-->
        </sdk:DataGrid>
    </Grid>
</controls:ChildWindow>

由于命名空间是控件所以我不能使用:

<UserControl.Resources>
    <viewmodel:ViewModelLocator x:Key="PersonLookupViewModel"/>     
</UserControl.Resources>

我的视图模型代码是:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Bixi.Atlas.Client.UI.OrganisationModule.Models.CostCenters;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.Prism.Commands;
using Bixi.Atlas.Client.UI.OrganisationModule.Views.CostCenters;

namespace Bixi.Atlas.Client.UI.OrganisationModule.ViewModels.CostCenters
{
    public class PersonLookupViewModel : ModelBase
    {
        List<PersonLookupModel> personLookupModel = new List<PersonLookupModel>();

        public List<PersonLookupModel> LoadPersonDetails()
        {
            personLookupModel.Add(new PersonLookupModel
            {
                PRS_PER_CODE = "1001",
                PRS_PER_NAME = "Raj",
                PRS_PER_FIRSTNAME = "Mehra",
                PRS_PER_COUNTRY = "India"

            });
            personLookupModel.Add(new PersonLookupModel
            {
                PRS_PER_CODE = "1002",
                PRS_PER_NAME = "Dheeraj",
                PRS_PER_FIRSTNAME = "Gupta",
                PRS_PER_COUNTRY = "India"
            });
            personLookupModel.Add(new PersonLookupModel
            {
                PRS_PER_CODE = "1003",
                PRS_PER_NAME = "Gaurav",
                PRS_PER_FIRSTNAME = "Puri",
                PRS_PER_COUNTRY = "India"
            });

            //this.GetPersonDetails = personLookupModel.AsQueryable();

            return personLookupModel;
        }

        IQueryable<CostCentersModel> mGetPersonDetails;
        public IQueryable<CostCentersModel> GetPersonDetails
        {
            get { return mGetPersonDetails; }
            set
            {
                mGetPersonDetails = value;
                OnPropertyChanged("GetPersonDetails");
            }
        }


        public DelegateCommand LoadCommand { get; private set; }



        //When the button is pressed in MainPage, executes method ExecuteOpenChildWindow  

        private MyDelegate _openChildWindow;
        public MyDelegate OpenChildWindow
        {
            get
            {
                if (_openChildWindow == null)
                    _openChildWindow = new MyDelegate(executeOpenChildWindow);
                return _openChildWindow;
            }

        }

        // New instance of ChildWindow. Sets the NameProperty of the ChildWindow equal to the Name entered in the MainPage.  
        Lookup cw;
        private void executeOpenChildWindow(object parameter)
        {

            cw = new Lookup(this);
            //MyNameCW = MyNameVM;
            cw.Show();
        }

        //When OK-button is pressed in ChildWindow  
        private MyDelegate _okChildWindow;
        public MyDelegate OkChildWindow
        {
            get
            {
                if (_okChildWindow == null)
                    _okChildWindow = new MyDelegate(OkSaveChildWindow);
                return _okChildWindow;
            }

        }

        //MainPage Address property is set to the value entered in the address textbox in Child Window. Child Window is closed.  

        private void OkSaveChildWindow(object parameter)
        {
            //MyAddressVM = MyAddressCW;
            cw.Close();
        }


        public PersonLookupViewModel()
        {
            LoadPersonDetails();

            LoadCommand = new DelegateCommand(() => LoadPersonDetails());
        }
    }
}

1 个答案:

答案 0 :(得分:2)

<controls:ChildWindow.Resources>
    <viewmodel:ViewModelLocator x:Key="PersonLookupViewModel"/>
</controls:ChildWindow.Resources>