Will
Will

Reputation: 3585

Is it possible to bind the property of a datacontext to the property of another datacontext

I have a Class which I use in a control for defining colors :

public class ColorModel : INotifyPropertyChanged {
    public Color Color{ get { ... } set { ... } }
}

I also have a class which I wish to use for defining two-color LinearGradient Brushes :

public class GradientModel : INotifyPropertyChanged {
    public Color First{ get { ... } set { ... } }
    public Color Last{ get { ... } set { ... } }
}

Both of these classes serve as DataContexts for the controls that are responsible for defining their respective values.

I want to be able to use the ColorModel that I have defined to dictate the value of the First and Last colors of the GradientModel ( using two separate controls, each housing a ColorModel as a DataContext ).

I'm endeavoring to adhere to the MVVM as closely as possible.

How can I go about accomplishing this task?

Upvotes: 1

Views: 101

Answers (3)

AnjumSKhan
AnjumSKhan

Reputation: 9827

  1. By rule, Binding will work only on DependencyProperty of DependencyObject.

  2. Make ColorModel, and GradientModel DependencyObject.

  3. Create a MainViewModel so that you can set up a Binding.

    public class MainViewModel
    {
        public ColorModel CM1 { get; set; }
        public ColorModel CM2 { get; set; }
    
        public GradientModel GM { get; set; }
    
        public MainViewModel()
        {
            CM1 = new ColorModel();
            CM2 = new ColorModel();
            GM = new GradientModel();
    
            Binding b1 = new Binding("Color");
            b1.Source = CM1;
            b1.Mode = BindingMode.OneWay;
            BindingOperations.SetBinding(GM, GradientModel.FirstProperty, b1);
    
            Binding b2 = new Binding("Color");
            b2.Source = CM2;
            b2.Mode = BindingMode.OneWay;
            BindingOperations.SetBinding(GM, GradientModel.LastProperty, b2);
        }
    }
    
    <TextBox x:Name="Ctrl1" HorizontalAlignment="Left" Height="23" Margin="32,48,0,0" TextWrapping="Wrap" Text="{Binding CM1.Color}" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="Ctrl2" HorizontalAlignment="Left" Height="23" Margin="32,103,0,0" TextWrapping="Wrap" Text="{Binding CM2.Color}" VerticalAlignment="Top" Width="120"/>
    

XAML ( I have used string type instead of Color type in your VMs for demo. )

    <TextBlock HorizontalAlignment="Left" Height="23" Margin="210,77,0,0" TextWrapping="Wrap" Text="{Binding GM.First}" VerticalAlignment="Top" Width="120" Background="#FFE8D7D7"/>
    <TextBlock HorizontalAlignment="Left" Height="23" Margin="352,77,0,0" TextWrapping="Wrap" Text="{Binding GM.Last}" VerticalAlignment="Top" Width="120" Background="#FFECD7D7"/>

Upvotes: 0

dotNET
dotNET

Reputation: 35380

Another way to accomplish this could be to define your GradientModel as follows:

public class GradientModel : INotifyPropertyChanged {
    public ColorModel First{ get { ... } set { ... } }
    public ColorModel Last{ get { ... } set { ... } }
}

That is, instead of defining the properties as Color, define them as ColorModel. Then in the constructor, subscribe to PropertyChanged event and update your members accordingly.

Note: You'll have to update your binding paths from First to First.Color and so on.

Upvotes: 2

ΩmegaMan
ΩmegaMan

Reputation: 31596

ColorModel that I have defined to dictate the value of the First and Last colors of the GradientModel

Within ColorModel instance, subscribe to the instance of GradientModel's INotifyPropertyChanged mechanism. Then within the subscription operation method just detect the property Color change event and when it occurs extract its value and update the properties of First and Last accordingly.

Upvotes: 0

Related Questions