假设我在WPF应用程序的TabControl上有十个文本框。我的目的是设置tabcontrol的IsReadOnly,使其上的每个文本框自动设置为ReadOnly。但TabControl似乎没有IsReadOnly属性。如何在WPF中的网格上设置文本框的只读属性?
我该如何做到这一点?
假设我在WPF应用程序的TabControl上有十个文本框。我的目的是设置tabcontrol的IsReadOnly,使其上的每个文本框自动设置为ReadOnly。但TabControl似乎没有IsReadOnly属性。如何在WPF中的网格上设置文本框的只读属性?
我该如何做到这一点?
一种方法可能是创建自己的tabcontrol。然后添加一个DependencyProperty IsReadOnly。如果你完成了,你可以将所有TextBoxes IsReadOnly绑定到你的TabItem IsReadOnly。
我能想到的最好的办法:附加只读属性到TabControl类型:
创建一个新的辅助类:
public static class TabControlHelper
{
private static readonly DependencyProperty IsReadOnlyProperty =
DependencyProperty.RegisterAttached("IsReadOnly", typeof(bool), typeof(TabControlHelper), new PropertyMetadata(false));
}
然后在您的XAML:
<TabControl TabControlHelper.IsReadOnly="true">...</TabControl>
然后您可以简单地在选项卡控件的每个文本框中继承此值。
你可以一个文本框样式添加到您的TabControl的资源:
<TabControl.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="IsReadOnly" Value="{Binding IsReadOnly}" />
</Style>
</TabControl.Resources>
谢谢你,大卫。我会试一试。 – user748149 2011-05-16 01:19:30