首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF Adorner Newbee

WPF Adorner Newbee
EN

Stack Overflow用户
提问于 2014-08-05 20:19:14
回答 1查看 348关注 0票数 0

使用VB.Net和WPF

我正在使用Overlaying Controls in WPF with Adorners上提供的代码(转换为VB.Net)

MainWindow.xaml

代码语言:javascript
复制
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Name="G1">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <TextBlock Text=" This is Parent Control " HorizontalAlignment="Center" VerticalAlignment="Center"/>
    <Button Grid.Row="1" Content="Show Child Control " HorizontalAlignment="Center" VerticalAlignment="Center" Click="Button_Click"/>
</Grid>
</Window>

代码隐藏

代码语言:javascript
复制
Class MainWindow 

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    Dim U1 As New UserControl1
    Using OverlayAdorner(Of UserControl).Overlay(G1, U1)

    End Using
End Sub

End Class

UserControl1.xaml

代码语言:javascript
复制
UserControl x:Class="UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid Background=" #44000000">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Grid Grid.Row="1" Background="White" HorizontalAlignment="Center" VerticalAlignment="Center">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <TextBlock Text=" This is Child Control " HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5"/>
        <TextBox Name="T1" Grid.Row="1" Margin="5"/>
        <Button Grid.Row="2" Content="Close Child Control " HorizontalAlignment="Center" VerticalAlignment="Center" Click="Button_Click" Margin="5"/>
    </Grid>
</Grid>

</UserControl>

代码隐藏

代码语言:javascript
复制
Public Class UserControl1

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)

End Sub

Public Property UserInput As String
    Get
        Return T1.Text
    End Get
    Set(value As String)
        T1.Text = value
    End Set
End Property
End Class

现在,当用户单击主窗口中的按钮时,usercontrol1应该作为装饰器打开,并允许用户在文本框中输入一些文本,当用户单击usercontrol1中的按钮时,它应该关闭并将文本返回到主窗口,在那里它将显示为消息框。

请帮助我取得与我在wpf中的新手一样的成就

EN

回答 1

Stack Overflow用户

发布于 2014-08-05 22:09:59

我试图写一个基于纯xaml的解决方案,您可以根据需要进行调整

在此示例中,有两个文本框,第一个是常规文本框,而其他弹出框在聚焦时会显示一个列表,并允许您从中选择一个项目

示例代码

代码语言:javascript
复制
<Grid>
    <Grid.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    </Grid.Resources>
    <StackPanel  HorizontalAlignment="Center"
                 VerticalAlignment="Center"
                 MinWidth="200">
        <TextBox Text="regular textbox" />
        <TextBox Text="{Binding SelectedItem,ElementName=list,TargetNullValue=select an item}">
            <TextBox.Triggers>
                <EventTrigger RoutedEvent="GotFocus">
                    <BeginStoryboard>
                        <Storyboard>
                            <BooleanAnimationUsingKeyFrames Storyboard.TargetName="button"
                                                            Storyboard.TargetProperty="IsChecked">
                                <DiscreteBooleanKeyFrame Value="true"
                                                         KeyTime="0:0:0" />
                            </BooleanAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </TextBox.Triggers>
        </TextBox>
    </StackPanel>
    <Border Background="#77000000"
            Visibility="{Binding IsChecked,ElementName=button,Converter={StaticResource BooleanToVisibilityConverter}}">
        <Grid HorizontalAlignment="Center"
              VerticalAlignment="Center"
              Width="300"
              Height="150">
            <ListBox x:Name="list">
                <sys:String>item 1</sys:String>
                <sys:String>item 2</sys:String>
                <sys:String>item 3</sys:String>
                <sys:String>item 4</sys:String>
                <ListBox.Triggers>
                    <EventTrigger RoutedEvent="MouseLeftButtonUp">
                        <BeginStoryboard>
                            <Storyboard>
                                <BooleanAnimationUsingKeyFrames Storyboard.TargetName="button"
                                                                Storyboard.TargetProperty="IsChecked">
                                    <DiscreteBooleanKeyFrame Value="False"
                                                             KeyTime="0:0:0" />
                                </BooleanAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </ListBox.Triggers>
            </ListBox>
            <ToggleButton IsChecked="False"
                          x:Name="button"
                          HorizontalAlignment="Right"
                          VerticalAlignment="Top"
                          Content="X"
                          ToolTip="Close" />
            <Grid.Effect>
                <DropShadowEffect Opacity=".5" />
            </Grid.Effect>
        </Grid>
    </Border>
</Grid>

边框内的网格内容是放置用户控件的位置

只需将此代码复制到新项目的主窗口并运行它。试一试,看看是否接近您的需求,我们可以根据您的需求来设计适合它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25138680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档