Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM binding properties and subproperties

I have a view model that inherits from a baseclass that has a property called IsReadOnly. In this view model i have a property called Customer and i'm binding the properties of the customer object to controls on my view.

However i also want to be able to bind the IsReadOnly to each control on my view also.

<TextBox x:Name="FirstNameTextBox" Grid.Column="1" Margin="2,2,0,2" Grid.Row="2" TextWrapping="Wrap" HorizontalAlignment="Left" Width="200" 
                         Text="{Binding FirstName, Mode=TwoWay}" IsReadOnly="{Binding MyViewModel.IsReadOnly}"/>

How can i go about using both these properties? here is my structure

public class MyViewModelBase { public bool IsReadonly { get;set;} }

public class MyViewModel { public Customer Customer { get; set; } }

public class Customer { public string FamilyName { get; set; } }

Cheers for any help

like image 711
BBurke Avatar asked May 12 '11 06:05

BBurke


1 Answers

Property traversing works with Binding too, so you can do the following to bind to IsReadonly property of the base object:

public class MyViewModel {
    public Customer Customer { get; set; }
}

public class Customer : Entity {
}

public class Entity {
    public bool IsReadonly { get;set;}
}

<Button IsEnabled="{Binding Customer.IsReadonly}" />

For the above example, I'm supposing your view is bound to an instance of "MyViewModel" and you probably already have property notification change on your properties.

like image 106
Hadi Eskandari Avatar answered Oct 07 '22 19:10

Hadi Eskandari