24

ComboBox非標準のドロップダウン配置でを作成しようとしています。基本的に、ドロップダウンをの下に配置しますが、左端ComboBoxではなく右端に揃えComboBoxます。

を使用して、通常はどのComboBoxように見えるかPlacementMode="Bottom"

左揃えのコンボボックス

私が欲しいもの:

コンボボックスを右揃え

Popup.PlacementModeのテンプレートでプロパティを試してみましたComboBoxが、可能な値のどれも私が望むことをしていないようです。できれば純粋なXAMLでそれを行う簡単な方法はありますか?

4

4 に答える 4

45

Expression Blendを開いたとき、数秒以内に解決策を思いつきました。

<Popup Placement="Left" VerticalOffset="{TemplateBinding ActualHeight}" 
       HorizontalOffset="{TemplateBinding ActualWidth}"

このアプリケーションは、xamlを手動で作成するよりも便利な場合がありますが、それほど頻繁ではありません。 ここに画像の説明を入力してください

于 2011-03-17T19:14:59.700 に答える
7

ここに示すように、ポップアップに「カスタム」配置モードを使用し、コールバックを宣言してポップアップコントロールを正しい位置に配置します。WPFComboBox 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) } };
    }
}

これがお役に立てば幸いです

于 2011-03-17T20:04:02.337 に答える
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>      
于 2011-10-27T12:37:15.523 に答える
2

少しハッキーですが、機能します。コンボボックスのスタイルを変更するだけです。

    <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>

ポップアップHorizo​​ntalOffsetは、PopupContent.ActualWidth-PlacementTarget.ActualWidthの値を取得する必要があります。その価値を得るために、私はCharlesPetzoldからこのトリックを使用しました。

于 2011-03-17T18:46:18.790 に答える