开发者

Binding Up Nested UserComponent To it ViewModel

I have a WFP Project and i am using MVVM Pattern. I have AddressView User control which i used in CustomerView UserControl.

<my:AddressVeiw Width="340" DataContext="AddressViewModel"/>

My AddressVeiw userControl has a AddressViewModel and CustomerView has a CustomerViewModel

Code for CustomerViewModel

        public DelegateCommand<object> SaveCommand { get; set; }

 
        private string firstN开发者_C百科ame;

        public string FirstName
        {
            get { return firstName; }
            set { 
                firstName = value;
                RaisePropertyChanged("FirstName");
                SaveCommand.RaiseCanExecuteChanged();
            }
        }

        private string lastName;

        public string LastName
        {
            get { return lastName; }
            set { 
                lastName = value;
                RaisePropertyChanged("LastName");
                SaveCommand.RaiseCanExecuteChanged();
            }
        }
        private AddressViewModel addressViewModel;

        public AddressViewModel AddressViewModel
        {
            get { return addressViewModel; }
            set { addressViewModel = value; }
        }

        private string middleName;

        public string Middlename
        {
            get { return middleName; }
            set
            {
                middleName = value;
                RaisePropertyChanged("MiddleName");
                SaveCommand.RaiseCanExecuteChanged();
            }
        }
        private string fullName;

        public string FullName
        {
            get { return fullName; }
            set { 
                fullName = value;
                RaisePropertyChanged("FullName");
            }
        }
        
            
        private void InitializeCommands()
        {
            SaveCommand = new DelegateCommand<object>(OnSaveCommand, CanSaveExcute);
        }

        private bool CanSaveExcute(object obj)
        {
            if (string.IsNullOrEmpty(firstName) ||string.IsNullOrEmpty(lastName))
                return false;
            return true;
        }

        private void OnSaveCommand(object obj)
        {
            FullName = FirstName + " " + LastName;
        }

    }

Code for AddressViewModel

    private ObservableCollection<Country> countryList = new ObservableCollection<Country>();

    public ObservableCollection<Country> CountryList
    {
        get { return countryList; }
        set { countryList = value; }
    }

    public DelegateCommand<object> SaveCommand { get; set; }

    private void Load()
    {
        try
        {
            CountryList = (new CountryRepository().GetAll());
        }
        catch (Exception ex)
        {

            OnSetStatusBarText("Error: " + ex.Message.ToString());
        }

    }
    private void OnSetStatusBarText(string message)
    {
        var evt = eventAgg.GetEvent<StatusBarMessageEvent>();
        evt.Publish(message);
    }
    private void InitializeCommands()
    {
        SaveCommand = new DelegateCommand<object>(OnSaveCommand, CanSaveExcute);
    }
    private bool CanSaveExcute(object obj)
    {

        return true;
    }

    private void OnSaveCommand(object obj)
    {

    }

Some how i can hook my AdddressViewModel To my AddressView, Customer Works fine... What Must be done to resolve this problem? Thanks


You need to use a binding expression for the DataContext of your AddressView. Instead of this...

<my:AddressVeiw Width="340" DataContext="AddressViewModel"/>

...try this...

<my:AddressVeiw Width="340" DataContext="{Binding AddressViewModel}"/>


You're close, but you need a binding:

<my:AddressVeiw Width="340" DataContext="{Binding AddressViewModel}"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜