禁用WPF中的所有文本框

时间:2015-04-03 10:45:00

标签: c# wpf

我有一种情况需要在页面上禁用我的所有文本框。 为此,我试图使用下面的代码

 private void DrpDwnSelectSemTimeTable_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
    var firstlecture = this.FirstLecture.Children.OfType<TextBox>();
    var secondlecture = this.SecondLecture.Children.OfType<TextBox>();
    var thirdlecture = this.ThirdLecture.Children.OfType<TextBox>();
    var fourthlecture = this.FourthLecture.Children.OfType<TextBox>();
    var fifthlecture = this.FifthLecture.Children.OfType<TextBox>();

                    foreach (var item in firstlecture)
                    {
                        item.IsReadOnly = true;
                    }
                    foreach (var item in secondlecture)
                    {
                        item.IsReadOnly = true;
                    }
                    foreach (var item in thirdlecture)
                    {
                        item.IsReadOnly = true;
                    }
                    foreach (var item in fourthlecture)
                    {
                        item.IsReadOnly = true;
                    }
                    foreach (var item in fifthlecture)
                    {
                        item.IsReadOnly = true;
                    }
}

任何我的XAML

    <Canvas>
            <WrapPanel x:Name="TimeTableMainWrapPanel" Canvas.Left="109" Canvas.Top="195" Height="601" Width="745>
                <TextBox x:Name="txtFirstLctrTime"  Height="24" TextWrapping="Wrap" Width="115"/>
                <TextBox x:Name="txtSecondLctrTime" Height="23" Canvas.Left="594" TextWrapping="Wrap" Canvas.Top="189" Width="115"/>

              <WrapPanel x:Name="TimeTableSubWrapPanel" Canvas.Left="109" Canvas.Top="195" Height="601" Width="745">
                 <WrapPanel x:Name="FirstLecture" Background="#00F0F8FF" Height="392" Width="133" Margin="0,0,10,0">
                    <TextBox x:Name="txtMondayFirstLctr" Width="133" Margin="0" Height="30" FontSize="13" VerticalContentAlignment="Center"/>
                    <TextBox x:Name="txtTuesdayFirstLctr" Width="133" Margin="0,38,0,0" Height="30" FontSize="13" VerticalContentAlignment="Center"/>
                 </WrapPanel>

                <WrapPanel x:Name="SecondLecture" Canvas.Top="220" Background="#00F0F8FF" Canvas.Left="270" Height="466" Width="133" Margin="8,0,10,0">
                    <TextBox x:Name="txtMondaySecondLctr" Width="133" Margin="0,38,0,0" Height="30" VerticalContentAlignment="Center"></TextBox>
                    <TextBox x:Name="txtTuesdaySecondLctr" Width="133" Margin="0,38,0,0" Height="30" VerticalContentAlignment="Center"></TextBox>
                </WrapPanel>

                <WrapPanel x:Name="ThirdLecture" Canvas.Top="219" Background="#00F0F8FF" Canvas.Left="415" Height="466" Width="133" Margin="8,0,10,0">
                    <TextBox x:Name="txtMondayThirdLctr" Width="133" Margin="0,38,0,0" Height="30" VerticalContentAlignment="Center"></TextBox>
                    <TextBox x:Name="txtTuesdayThirdLctr" Width="133" Margin="0,38,0,0" Height="30" VerticalContentAlignment="Center"></TextBox>
                </WrapPanel>

                <WrapPanel x:Name="FourthLecture" Canvas.Top="221" Background="#00F0F8FF" Canvas.Left="562" Height="466" Width="133" Margin="8,0,0,0">
                    <TextBox x:Name="txtMondayFourthLctr" Width="133" Margin="0,38,0,0" Height="30" VerticalContentAlignment="Center"></TextBox>
                    <TextBox x:Name="txtTuesdayFourthLctr" Width="133" Margin="0,38,0,0" Height="30" VerticalContentAlignment="Center"></TextBox>
                </WrapPanel>

                <WrapPanel x:Name="FifthLecture" Canvas.Top="220" Background="#00F0F8FF" Canvas.Left="710" Height="466" Width="135" Margin="18,0,0,0">
                    <TextBox x:Name="txtMondayFifthLctr" Width="133" Margin="0,38,0,0" Height="30" VerticalContentAlignment="Center"></TextBox>
                    <TextBox x:Name="txtTuesdayFifthLctr" Width="133" Margin="0,38,0,0" Height="30" VerticalContentAlignment="Center"></TextBox>
                </WrapPanel>
            </WrapPanel>
 </Canvas>

有更好的方法吗?

2 个答案:

答案 0 :(得分:0)

<WrapPanel x:Name="TimeTableMainWrapPanel" IsEnabled="False">

答案 1 :(得分:0)

将所有文本框放在堆栈面板下,如下所示:

<StackPanel Orientation="Horizontal">
    <StackPanel x:Name="TextboxContainer" Orientation="Horizontal">
        <!-- All textboxes here -->
    </StackPanel>
    <Button Content="Cancel" />
</StackPanel>

现在,当您点击按钮或任何事件被触发时,内部事件会禁用如下所示的堆栈面板:

TextboxContainer.IsReadonly = true;

因此,您不需要遍历页面上的所有控件,甚至不需要遍历每个文本框。所以对我来说,这是实现目标的更优化和最快捷的方式。

注意:额外的stackpanel会在那里,所以先看看你的可行性!

问我你是否有任何问题。