【问题标题】:Creating an overlay to disable all other form elements创建覆盖以禁用所有其他表单元素
【发布时间】:2015-06-12 05:20:58
【问题描述】:

我想编写一个用户控件来输入新密码。在我看来,这应该显示为“更改密码”按钮,当用户单击该按钮时,它应该被 PasswordBox 和两个按钮 SaveDiscard 替换。当用户单击Save 按钮时,应调用DataContext 中的一个方法,并将新密码作为参数。

我的问题是,当密码用户控件处于“更改密码状态”并且我必须在下面显示一个虚拟键盘时,还必须有类似覆盖的东西,它会禁用视图的其余部分覆盖在PasswordBox下方。

我正在寻找提示,要走哪条路。我认为Adorner 应该这样做,但我是 WPF 新手,不知道这是否正确。

我不想创建弹出窗口。我想在那里显示“更改密码”按钮所在的PasswordBox

编辑:

现在,我有这样的:

<TextBlock Grid.Column="0" Grid.Row="2" Text="{lex:LocText Password}"/>
<Button Grid.Column="2" Grid.Row="2" Visibility="{Binding IsPasswordBoxVisible, Converter={StaticResource NegatedBooleanVisibilityConverter}}" Command="{Binding ShowPasswordBoxCommand}" Content="{lex:LocText SetNewPassword}"/>
<PasswordBox Grid.Row="2" Grid.Column="2" Visibility="{Binding IsPasswordBoxVisible, Converter={StaticResource BooleanVisibilityConverter}}" ... />

PasswordBox 应该出现在它现在所在的相同位置,但位于覆盖层的顶部(旁边有两个按钮“保存”和“取消”)。

【问题讨论】:

    标签: c# wpf xaml adorner


    【解决方案1】:

    这需要附加属性 Panel.ZIndex 将“覆盖”放在其他所有内容之上。您可以在代码中设置“覆盖”的可见性或使用 DataTrigger。

    如果您在布局中使用 Canvas 或 Grid,则将控件置于更高的 ZIndex 顶部

    <Grid x:Name="Overlay" Panel.ZIndex="1000" Visibility="Collapsed">
        <Grid.Background>
          <SolidColorBrush Color="Black" Opacity=".5"/>
        </Grid.Background>
    
        <!-- Add controls as needed -->
      </Grid>
    
      <!-- Use whatever layout you need -->
      <ContentControl x:Name="MainContent" />
    
    </Grid>
    

    更新代码:

     <Grid x:Name="Overlay" Panel.ZIndex="9999" Visibility="Visible">
                <Grid.Background>
                    <SolidColorBrush Color="Black" Opacity=".25"/>
                </Grid.Background>           
                <Grid Height="100" Width="300" Background="WhiteSmoke">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="5*"/>
                        <ColumnDefinition Width="5*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="5*"/>
                        <RowDefinition Height="5*"/>
                    </Grid.RowDefinitions>
                    <TextBlock Text="Password" Width="120" Height="50"/>
                    <PasswordBox Grid.Column="1" Width="120" Height="30"/>
                    <Button Grid.Row="1" Grid.Column="1" Margin="5" Content="Submit"/>                
                </Grid>
            </Grid>
    
            <!-- Use whatever layout you need -->
            <ContentControl x:Name="MainContent" />
    
        </Grid> 
    

    【讨论】:

    • 没有必要使用Panel.ZIndex。您可以在 XAML 的底部添加一个Rectangle,它已经位于 Z 平面中其他所有内容的顶部。
    • 感谢您的回复。我怎样才能将我的PasswordBox 放在那个叠加层上,以便它出现在“更改密码”按钮所在的位置。我编辑了我的问题。
    • 我已经用你的代码更新了覆盖控制。根据您的场景触发可见性。
    猜你喜欢
    • 2020-09-27
    • 2013-10-19
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多