开发者

How to set readonly property of textboxes on a Grid in WPF?

Supp开发者_StackOverflowose I have ten textBoxes on a TabControl in WPF application. My purpose is to set the IsReadOnly of the tabcontrol so that every textbox on it is set to be ReadOnly automatically. But TabControl does not seem to have a IsReadOnly property.

How can I make that happen?


one way could be to create your own tabcontrol. then add a DependencyProperty IsReadOnly. if you are done, you can bind all TextBoxes IsReadOnly to your TabItem IsReadOnly.


You could add a TextBox style to your TabControl's resources:

        <TabControl.Resources>
            <Style TargetType="{x:Type TextBox}">
                <Setter Property="IsReadOnly" Value="{Binding IsReadOnly}" />
            </Style>
        </TabControl.Resources>


best way I can think of: attach the ReadOnly property to the TabControl type:

create a new helper class:

public static class TabControlHelper
{
    private static readonly DependencyProperty IsReadOnlyProperty =
        DependencyProperty.RegisterAttached("IsReadOnly", typeof(bool), typeof(TabControlHelper), new PropertyMetadata(false));
}

then in your xaml:

<TabControl TabControlHelper.IsReadOnly="true">...</TabControl>

then you can simply inherit this value in every textbox in your tab control.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜