开发者

Two views referring same view model

I'm using two views which refers same view model. Both of my views contain a text box that binds to a value in the view model. My problem is that, if I change the value of textbox in one GUI, its not reflecting in another. What should I do to achieve this?

This is my view model

    public class ProductViewModel:INotifyPropertyChanged
    { 
        private int machineheight;

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string prop开发者_开发技巧ertyName)
        {

            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        public int MachineHeight
        {
            get
            {
                return this.machineheight;
            }
            set
            {
                this.machineheight = value;
                RaisePropertyChanged("MachineHeight");
            }
        }

        public ProductViewModel()
        {
        }

        private ICommand mUpdater;
        public ICommand UpdateCommand
        {
            get
            {
                if (mUpdater == null)
                    mUpdater = new Updater();
                return mUpdater;
            }
            set
            {
                mUpdater = value;
            }
        }

        private class Updater : ICommand
        {
            #region ICommand Members
            public bool CanExecute(object parameter)
            {
                return true;
            }
            public event EventHandler CanExecuteChanged;
            public void Execute(object parameter)
            {
                SecondWindow w = new SecondWindow();
                w.Show();
            }
            #endregion
        }
    }
}

The second window is another GUI. Once I click update button, second window opened. But the value that I have changed in first UI is not updated in the new window.

My Xaml is similar for both UI..

<Window x:Class="WPFDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:local="clr-namespace:WPFDemo"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <local:ProductViewModel/>
</Window.DataContext>


<Grid  Height="307" Width="480" Initialized="Grid_Initialized">
    <Button Content="Update" Height="32" HorizontalAlignment="Left" Margin="165,158,0,0" Name="button1" VerticalAlignment="Top" Width="114" Command="{Binding Path=UpdateCommand}"/>
    <TextBox Height="42" HorizontalAlignment="Left" Margin="125,82,0,0" Name="textBox1" VerticalAlignment="Top" Width="169"  Text= "{Binding Path= MachineHeight, Mode=TwoWay}" />
    </Grid>
</Window>

I actually don't know what is the problem.. thanks


<Window.DataContext>
    <local:ProductViewModel/>
</Window.DataContext>

hi, if you put this in your 2 views, then each one has its own viewmodel. so you will never see any changes. you have to set the datacontext from your first view to your second view. Btw for your ICommand implementation look at some mvvm frameworks for easier implementations, eg RelayCommand, DelegateCommand.

For your actual implementation you can add the following to your xaml and ViewModel(CommandParameter) then it works.

<Button Content="Update" Height="32" HorizontalAlignment="Left" Margin="165,158,0,0" Name="button1" VerticalAlignment="Top" Width="114" Command="{Binding Path=UpdateCommand}"
            CommandParameter="{Binding .}"/>

public void Execute(object parameter)
        {
            SecondWindow w = new SecondWindow();
            w.DataContext = parameter;
            w.Show();
        }


There are a hundred things that can go wrong in this scenario, and one of my long-standing gripes with XAML-based databinding is that the MS tools give you precious little help figuring out which of those hundred things it is. This is especially the case if you're new to databinding, but even folks who've been doing it for years can spend obnoxious hours tracking down databinding issues.

Some things to check:

(1) Confirm that your databindings are two-way.
(2) Look in your debug output window to see if there are any error messages there.
(3) Set an IValueConverter in your databinding, and set a breakpoint in the converter to see what data is being passed where and when.
(4) Confirm that the data in the ViewModel is actually being updated.
(5) Confirm that the ViewModel implements INotifyPropertyChanged, and that the PropertyChanged event is firing.
(6) Post your actual code here so folks can look at it.

And so forth.

Hope this helps.


It must work if the ViewModel implements INotifyPropertyChanged.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜