我正在尝试使用非标准下拉列表创建ComboBox
。基本上,我希望下拉列表低于ComboBox
,但与ComboBox
的右边缘而不是左边缘对齐。
使用ComboBox
:
PlacementMode="Bottom"
我想要的是什么:
我尝试使用Popup.PlacementMode
模板中的ComboBox
属性进行播放,但是没有一个可能的值似乎符合我的要求。有一种简单的方法可以做到这一点,最好是在纯XAML中吗?
答案 0 :(得分:41)
当我打开Expression Blend时,我在几秒钟内就提出了解决方案:
<Popup Placement="Left" VerticalOffset="{TemplateBinding ActualHeight}"
HorizontalOffset="{TemplateBinding ActualWidth}"
有时候这个应用程序比手工编写xaml更有用,但不常见。
答案 1 :(得分:4)
我会使用PopUp的“自定义”放置模式并声明一个回调函数,将弹出控件置于正确的位置,如下所示:WPF ComboBox DropDown Placement
查看此处的示例是否适合您:
public class TestComboBox : ComboBox
{
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var popup = (Popup)Template.FindName("PART_Popup", this);
popup.Placement = PlacementMode.Custom;
popup.CustomPopupPlacementCallback += (Size popupSize, Size targetSize, Point offset) =>
new[] { new CustomPopupPlacement() { Point = new Point (targetSize.Width-popupSize.Width, targetSize.Height) } };
}
}
希望这有帮助,尊重
答案 2 :(得分:4)
有人可以发布完整的xaml代码吗?
我尝试了以下内容:
<ComboBox Grid.Column="1" Height="24" Width="20" HorizontalAlignment="Right"
VerticalAlignment="Top"
Name="comboBox2"
ItemsSource="{Binding Source={StaticResource FilterTypes}}"
SelectedValue="{Binding Path=SelectedType, Mode=TwoWay}" >
<ComboBox.Template>
<ControlTemplate>
<Popup Placement="Left" VerticalOffset="{TemplateBinding ActualHeight}"
HorizontalOffset="{TemplateBinding ActualWidth}" />
</ControlTemplate>
</ComboBox.Template>
</ComboBox>
...经过一些工作和测试后,我找到了一个很好的解决方案......
<ComboBox.Style>
<Style TargetType="ComboBox" >
<Setter Property="Popup.FlowDirection" Value="RightToLeft"/>
</Style>
</ComboBox.Style>
答案 3 :(得分:2)
它有点hacky,但确实有效。你只需改变组合框样式。
<Grid Height="40">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<FrameworkElement Name="dummy" Visibility="Collapsed">
<FrameworkElement.RenderTransform>
<TransformGroup x:Name="xformgrp">
<TranslateTransform X="{Binding ElementName=PopupContent, Path=ActualWidth}" />
<ScaleTransform ScaleX="-1" />
<TranslateTransform X="{Binding ElementName=chk, Path=ActualWidth}" />
</TransformGroup>
</FrameworkElement.RenderTransform>
</FrameworkElement>
<CheckBox Name="chk" HorizontalAlignment="Center">checkthisout</CheckBox>
<Popup IsOpen="{Binding IsChecked, ElementName=chk}" PlacementTarget="{Binding ElementName=chk}" Placement="Bottom" HorizontalOffset="{Binding ElementName=dummy, Path=RenderTransform.Value.OffsetX}">
<TextBlock Name="PopupContent" Foreground="Yellow" Background="Blue">yeah long popupcontent</TextBlock>
</Popup>
</Grid>
</Grid>
弹出窗口HorizontalOffset只需要获取PopupContent.ActualWidth-PlacementTarget.ActualWidth的值。为了获得该值,我使用了this trick from Charles Petzold。