MVVM 绑定属性和子属性

发布于 2024-11-06 15:30:25 字数 636 浏览 0 评论 0原文

我有一个继承自基类的视图模型,该基类具有名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

并安 2024-11-13 15:30:25

属性遍历也适用于 Binding,因此您可以执行以下操作来绑定到基础对象的 IsReadonly 属性:

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}" />

对于上面的示例,我假设您的视图绑定到“MyViewModel”的实例,并且您可能已经有了属性通知改变你的财产。

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.

最近可好 2024-11-13 15:30:25

我假设您的 MyViewModel 继承自 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; } }

我还假设您的视图 DataContext 是 MyViewModel 的实例,如果不让我知道:)您的绑定应该如下所示:

<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}"/>

编辑:如果您的 TextBox 的 DataContext 是 Customer 属性,则必须在您的 Binding 中使用relativeSource为只读

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文