【发布时间】:2012-07-16 15:47:03
【问题描述】:
可以在 XAML 中创建一个可以通过单击按钮打开的无模式窗口/弹出窗口。我需要能够并排看到父窗口和子无模块窗口。 无模窗口最初需要适合我的网格,并且需要在单击它时扩展到父级。
弹出控件有多个错误。展开时弹出控件的高度不是 100% 的屏幕高度,它只适用于 75% 的屏幕高度。 此外,弹出控件加载数据需要太多时间。
这是我如何设置具有 Popup 的扩展器控件的样式。
<Style TargetType="Expander" x:Key="NotesSlider">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Expander" >
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Grid}} ,Path=ActualWidth}"
Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Grid}} ,Path=ActualHeight}"
x:Name="GridMain">
<Border x:Name="popupBorder" BorderBrush="White">
<!--<Popup IsOpen="True" PopupAnimation="Slide" x:Name="SlideOut" StaysOpen="True"
Placement="Relative" AllowsTransparency="True" Grid.Column="0" Width="Auto" Height="Auto">-->
<controls:PopupNonTopmost IsOpen="True" PopupAnimation="None" Placement="Relative" AllowsTransparency="True" StaysOpen="True"
Topmost="False" x:Name="SlideOut" Panel.ZIndex="10000" >
<Grid Margin="0 0 0 10" x:Name="gridContainer">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" x:Name="NoteHeaderRow"/>
<RowDefinition Name="NotesContentRow" Height="0"/>
</Grid.RowDefinitions>
<Border
Name="NoteBorder"
Grid.Row="0"
Background="{StaticResource NormalBrush}"
BorderBrush="{StaticResource NormalBorderBrush}"
BorderThickness="1"
CornerRadius="0" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="20" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<ContentPresenter
Grid.Column="0"
Margin="4"
ContentSource="Header"
RecognizesAccessKey="True" />
<ToggleButton
IsChecked="{Binding Path=IsExpanded,Mode=TwoWay,
RelativeSource={RelativeSource TemplatedParent}}"
OverridesDefaultStyle="True"
Grid.Column="1"
Template="{StaticResource VerticalExpanderToggleButton}"
Background="{StaticResource NormalBrush}" />
<ToggleButton x:Name="toggleMax"
OverridesDefaultStyle="True"
Grid.Column="2"
Template="{StaticResource SliderToggleButton}"
Background="{StaticResource NormalBrush}" />
</Grid>
</Border>
<Border
Name="NoteContent"
Grid.Row="1"
Background="{StaticResource WindowBackgroundBrush}"
BorderBrush="{StaticResource SolidBorderBrush}"
BorderThickness="1,0,1,1"
CornerRadius="0" >
<ContentPresenter Margin="4" x:Name="content"/>
</Border>
</Grid>
</controls:PopupNonTopmost>
<!--</Popup>-->
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True" SourceName="toggleMax">
<Setter TargetName="SlideOut" Property="PlacementTarget" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"/>
<Setter TargetName="SlideOut" Property="Width" Value="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Width,Converter={StaticResource PopupWidthConverter}}"/>
<Setter TargetName="SlideOut" Property="Height" Value="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height,Converter={StaticResource PopupHeightConverter}}"/>
<Setter TargetName="popupBorder" Property="BorderBrush" Value="Black"/>
<!--<Setter TargetName="SlideOut" Property="Width" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}},Path=Width}"/>
<Setter TargetName="SlideOut" Property="Height" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}},Path=ActualHeight}"/>-->
</Trigger>
<Trigger Property="IsChecked" Value="False" SourceName="toggleMax">
<Setter TargetName="SlideOut" Property="PlacementTarget" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Grid}}}"/>
<Setter TargetName="SlideOut" Property="Width" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Grid}},Path=Width}"/>
<Setter TargetName="SlideOut" Property="Height" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Grid}},Path=Height}"/>
</Trigger>
<Trigger Property="IsExpanded" Value="True">
<Setter TargetName="NotesContentRow" Property="Height" Value="{Binding ElementName=Content,Path=DesiredHeight}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="NoteBorder" Property="Background"
Value="{StaticResource DisabledBackgroundBrush}" />
<Setter TargetName="NoteBorder" Property="BorderBrush"
Value="{StaticResource DisabledBorderBrush}" />
<Setter Property="Foreground"
Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
【问题讨论】:
-
你尝试了什么?为什么简单的
new PopupWindow().Show()不起作用? -
Vlad - 弹出窗口有效。问题是当我删除弹出窗口的宽度属性时,它将弹出窗口的高度设置为父窗口的高度,但是当我将弹出窗口的宽度设置为父窗口的宽度时,它只设置为父窗口的 75% 高度.弹出窗口也需要很长时间才能加载。
-
好,我们来一一解决问题。 (1) 你想要的高度是多少?为什么要删除
Width属性? -
(2) 那么,如何加载数据?有两种常用方法:要么首先将数据加载到某些内部结构(可能在后台线程中),要么打开一个仅包含文本“正在加载...”的弹出窗口,启动一个加载数据的后台线程,然后在弹出窗口中显示它们(编组到 UI 线程)。
-
Vlad , 1. 我希望弹出控件的高度是用户的屏幕尺寸。 2. 我创建了一个自定义扩展器控件。此自定义扩展器的控件模板包含 PopUp 控件。