1

For example, i wrote this code:

xaml1:

<UserControl x:Class="Aplikacja_desktopowa.CustomerView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
         xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
    <Grid x:Name="LayoutRoot" Background="White" Height="150" Width="300" 
        DataContext="{Binding Source={StaticResource Locator}, Path=CustomerViewModel}">
        <Button Content="Button" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Command="{Binding LogowanieCommand}"/>
        <Button Content="Button"  IsEnabled="{Binding IsEnabled}" HorizontalAlignment="Left" Margin="112,104,0,0" VerticalAlignment="Top" Width="76"/>
    </Grid>
</UserControl>

Xaml2:

<Window x:Class="Aplikacja_desktopowa.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:Aplikacja_desktopowa"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{Binding Source={StaticResource Locator}, Path=[MainPageViewModel]}">
    <StackPanel>
        <TextBlock Text="{Binding Path=BannerText}" FontFamily="Verdana" FontSize="18" FontWeight="Bold" HorizontalAlignment="Center" />
        <Button Content="Button" IsEnabled="{Binding Source={StaticResource Locator}, Path=CustomerViewModel.IsEnabled}"/>
        <my:CustomerView />
    </StackPanel>
</Window>

View Model with property

public class CustomerViewModel : INotifyPropertyChanged{
    private DelegateCommand _przycisk;
    public ICommand LogowanieCommand
    {
        get
        {
            if (_przycisk == null)
                _przycisk = new DelegateCommand(funkcja1, funkcja2);
            return _przycisk;
        }
    }

    public void funkcja1()
    {
        MessageBox.Show("Odblokowywanie przycisku");
        IsEnabled = true;
    }
    public bool funkcja2()
    {
        return true;
    }

    private bool _isEnabled;

    public bool IsEnabled
    {
        get { return _isEnabled; }
        set
        {
            _isEnabled = value; 
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

It is something like this:

view1 ({binding IsEnabled) ----- view_model1_with_property_IsEnabled 
                                           |
                                           |
                                           |
                                           |
                                           |
main_view ( with Staticresource view_model1,IsEnabled)

I tried everything, but its not working ;((

How to notice propertychanged in both xamls?

Question clarification

Ok, so, i have two separate viewmodels, two separate views in wfp ( simple mvvm template ) When i click at button, it will set property IsEnabled at true. Its ok, notifypropertychange is working. But when i wanna notify property in different view ( for example i want unlock button in view1 when i click button in view2 )

<Button Content="Button" IsEnabled="{Binding Source={StaticResource Locator}, 
Path=CustomerViewModel.IsEnabled}"/>

it isnt, working ;( I looking for the easiest way to make sth like this in simple mvvm


Sheridan, i made a property in ViewModelLocator and its working, like i wanted, thx ... but when im implementing bigger application, in ViewModelLocator i will get a lot of methods, is there nicer solution ?

Community
  • 1
  • 1

2 Answers2

2

You probably want to have a look at using a framework like galasoft's mvvm light, it provides a messaging class that lets you pass objects between view models.

MVVM Light Messenger - Sending and Registering Objects

Community
  • 1
  • 1
Wombelite
  • 302
  • 1
  • 6
1

There are a number of solutions to this problem, but one of them is to use a parent view model that has access to the other view models. The simplest fix would be to define your bool IsEnabled property in the parent view model and then to bind directly to this property from the two child view models:

<Button Content="Button" IsEnabled="{Binding DataContext.IsEnabled, RelativeSource=
    {RelativeSource AncestorType={x:Type YourXmlNamespacePrefix:YourParentView}}}" />

This RelativeSource Binding will look for a property named IsEnabled in the object that is set as the DataContext of a parent or ancestor of this Button control of type YourParentView. Obviously, you'll need to update the names to those in your code.

Sheridan
  • 68,826
  • 24
  • 143
  • 183