开发者

MVVM binding properties and subproperties

I have a view model that inherits from a baseclass that has a property called IsReadOnly. In this v开发者_如何学编程iew 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


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.


i assume that your MyViewModel inherit from MyViewModelBase.

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

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

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

i also assume that your view DataContext is an instance of MyViewModel, if not let me know :) your binding should be like the following:

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

EDIT: if the DataContext of your TextBox is the Customer Property, you have to use RelativeSource in your Binding to IsReadOnly

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜