我真的在寻找一种好属性,以便在调整窗口大小时使Popup跟随扩展器。
将扩展器和弹出窗口定义为样式,但是如果扩展器随窗口向右移动而不是弹出窗口
如果我们看一下控件模板:
<ControlTemplate x:Key="RightExpander" TargetType="{x:Type Expander}">
<Border SnapsToDevicePixels="true" Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
CornerRadius="3">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="0"/>
</Grid.RowDefinitions>
<ToggleButton Width="{TemplateBinding ActualWidth}" Margin="0,1,0,1" MinHeight="0" MinWidth="30" x:Name="HeaderSite"
Style="{StaticResource LeftPanelCollpasedButtonStyle}" Content="{TemplateBinding Header}"
ContentTemplate="{TemplateBinding HeaderTemplate}"
ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"
FontFamily="{TemplateBinding FontFamily}" FontSize="{TemplateBinding FontSize}"
FontStretch="{TemplateBinding FontStretch}" FontStyle="{TemplateBinding FontStyle}"
FontWeight="{TemplateBinding FontWeight}" Foreground="{TemplateBinding Foreground}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
Padding="{TemplateBinding Padding}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
IsChecked="{Binding Path=IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" DockPanel.Dock="Top"/>
<Popup x:Name="popup" AllowsTransparency="False" Margin="0,1,0,1" IsOpen="False" Placement="Bottom" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<Grid >
<ContentControl x:Name="shadow" Visibility="Collapsed" HorizontalAlignment="Stretch" Style="{DynamicResource ShadowTemplate}"
VerticalAlignment="Stretch" Content="ContentControl" Opacity="1"
Margin="2,0,2,0" Foreground="#FF000000"/>
<ContentPresenter Opacity="1" MinWidth="220" HorizontalAlignment="Left" Margin="0,0,0,0" x:Name="ExpandSite" Focusable="false" Visibility="Collapsed" VerticalAlignment="Top" />
</Grid>
</Popup>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="true">
<Setter TargetName="shadow" Property="Visibility" Value="Visible"/>
<Setter TargetName="popup" Property="Placement" Value="Right"/>
<Setter TargetName="popup" Property="HorizontalOffset" Value="-5"/>
<Setter TargetName="popup" Property="IsOpen" Value="true"/>
<Setter TargetName="ExpandSite" Property="Margin" Value="5,5,5,5"/>
<Setter Property="Visibility" TargetName="ExpandSite" Value="Visible"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
<Setter TargetName="shadow" Property="Visibility" Value="Visible"/>
<Setter TargetName="popup" Property="Placement" Value="Right"/>
<Setter TargetName="popup" Property="HorizontalOffset" Value="-5"/>
<Setter TargetName="popup" Property="IsOpen" Value="true"/>
<Setter TargetName="ExpandSite" Property="Margin" Value="5,5,5,5"/>
<Setter Property="Visibility" TargetName="ExpandSite" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
定义了Popup的行指示“ Placement”和“ PlacementTarget”,但是无论我如何更改,它都不会对行为产生影响。 我认为我需要一些触发器,但是我不知道从哪里开始。如果有人可以帮助我,那就太好了。
我以这种方式在代码中使用controltemplate:
<Expander Header="MyExpander" Grid.Row="0" Style="{DynamicResource OutlookStyleExpanderStyle}" IsExpanded="true", Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" x:Name="proc" HorizontalAlignment="Left" Width="350" Margin="0,0,0,0">
<StackPanel Orientation="Vertical" Grid.Row="0" >
<StackPanel Orientation="Horizontal">
<Label FontSize="9" >LabelA</Label>
<TextBox Width="30"/>
<Label FontSize="9" >LabelB</Label>
<TextBox Width="30"/>
</StackPanel>
</Expander>