MVVM 绑定属性和子属性
我有一个继承自基类的视图模型,该基类具有名为 IsReadOnly 的属性。 在此视图模型中,我有一个名为 Customer 的属性,并且我将客户对象的属性绑定到我的视图上的控件。
不过,我也希望能够将 IsReadOnly 绑定到我视图上的每个控件。
<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}"/>
我该如何使用这两个属性? 这是我的结构
public class MyViewModelBase { 公共布尔IsReadonly {获取;设置;} }
公共类 MyViewModel { 公共客户客户{得到;放; } }
公共类客户 { 公共字符串 FamilyName { 获取;放; } 干杯
为任何帮助
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
属性遍历也适用于 Binding,因此您可以执行以下操作来绑定到基础对象的 IsReadonly 属性:
对于上面的示例,我假设您的视图绑定到“MyViewModel”的实例,并且您可能已经有了属性通知改变你的财产。
Property traversing works with Binding too, so you can do the following to bind to IsReadonly property of the base object:
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.
我假设您的 MyViewModel 继承自 MyViewModelBase。
我还假设您的视图 DataContext 是 MyViewModel 的实例,如果不让我知道:)您的绑定应该如下所示:
编辑:如果您的 TextBox 的 DataContext 是 Customer 属性,则必须在您的 Binding 中使用relativeSource为只读
i assume that your MyViewModel inherit from MyViewModelBase.
i also assume that your view DataContext is an instance of MyViewModel, if not let me know :) your binding should be like the following:
EDIT: if the DataContext of your TextBox is the Customer Property, you have to use RelativeSource in your Binding to IsReadOnly