2016-07-13 10 views
1

Ich habe eine Combobox mit Elementen breiter als die tatsächliche Combobox Breite, die Drop-Down-Lecks auf der rechten Seite. Wie können wir eine Combo-Box Pop-up machen verschüttet die linke statt der rechten Seite.Wie man ein Wpf Combobox Popup macht Links

Die FlowDirection tut es nicht.

Die im vorgeschlagenen Thread vorgeschlagene Lösung würde den Pfeil des Kombinationsfelds auf der linken Seite darstellen. Ich brauche es, um rechts zu bleiben.

<ComboBox.Style> 
     <Style TargetType="ComboBox" >          
      <Setter Property="Popup.FlowDirection" Value="RightToLeft"/>     
     </Style> 
    </ComboBox.Style> 
+1

Dieser Thread helfen könnten: http://stackoverflow.com/questions/5340640/wpf-combobox-popup-placement-bottom-and-aligned-to-the-right -edge – geostocker

+0

Ich bin bei der Suche auf diesen Thread gestoßen, aber er erreicht nicht das, wonach ich suche. – TheOne

+0

Irgendwelche Ideen Jungs? – TheOne

Antwort

2

Ich schlage vor, Sie die Standard ComboBox Kontrolle zu erweitern, um seine Popup Kontrolle zu manipulieren. Meiner Meinung nach ist dies die einfachste und schnellste Lösung für Ihr Problem.

public class LeftComboBox : ComboBox 
{ 
    public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 

     Popup popup = GetTemplateChild("PART_Popup") as Popup; 
     popup.Placement = PlacementMode.Custom; 
     popup.CustomPopupPlacementCallback = new CustomPopupPlacementCallback(CustomPopupPlacementCallback); 
    } 

    private static CustomPopupPlacement[] CustomPopupPlacementCallback(Size popupSize, 
     Size targetSize, Point offset) 
    { 
     Point point = new Point(targetSize.Width - popupSize.Width, targetSize.Height); 

     List<CustomPopupPlacement> customPopupPlacement = 
      new List<CustomPopupPlacement>(); 

     customPopupPlacement.Add(new CustomPopupPlacement(point, PopupPrimaryAxis.Vertical)); 

     return customPopupPlacement.ToArray(); 
    } 
} 

Durch die Standard-ComboBox Kontrolle erstreckt Sie die OnApplyTemplate außer Kraft setzen kann, und Sie können seine Popup zwingen PlacementMode die Gewohnheit zu verwenden.

Dann können Sie Ihre eigenen CustomPopupPlacementCallback schreiben. Mine ändert nur den horizontalen Offset Popup und bewegt ihn nach links.

Ich hoffe, es kann Ihnen helfen.

0

Ich schlage vor, Sie das einmal mit Behavior.I'm Zugabe Beispiel mit MenuItem aber das gleiche für jede Kontrolle mit PopUp (wie ComboBox)

Auf Ihrem XAML das Verhalten auf den Menüpunkt anhängen hinzufügen:

 <Menu> 
      <MenuItem ItemsSource="{Binding GlanceOptions, Mode=OneWay}"> 

       <interactivity:Interaction.Behaviors> 
        <behaviors:MenueItemSubMenuPlacementBehavior /> 
       </interactivity:Interaction.Behaviors> 

       <MenuItem.Header> 
        <Border      
         Cursor="Arrow" 
         BorderThickness="1,1,1,0" > 
         <Path x:Name="DropDownIcon" 
             VerticalAlignment="Center" 
             Data="M0,0L2,3 4,0z" 
             /> 
        </Border> 
       </MenuItem.Header> 
      </MenuItem > 
     </Menu>   

Das Verhalten Code:

 public class MenueItemSubMenuPlacementBehavior : Behavior<MenuItem> 
     { 
      private PlacementMode _previousPlacementMode; 
      private CustomPopupPlacementCallback _previousCallback; 
      private Popup _menuItemPopUp; 

      protected override void OnAttached() 
      { 
       base.OnAttached(); 

       Initialize(); 
      } 

      private void Initialize() 
      { 
       if (AssociatedObject == null) 
        return; 

       if (_menuItemPopUp == null) 
       { 
        _menuItemPopUp = AssociatedObject?.Template?.FindName("PART_Popup", AssociatedObject) as Popup; 

        if (_menuItemPopUp == null) 
        { 
         AssociatedObject.Loaded -= AssociatedObject_Loaded; 
         AssociatedObject.Loaded += AssociatedObject_Loaded; 
         return; 
        } 

        _previousPlacementMode = _menuItemPopUp.Placement; 
        _previousCallback = _menuItemPopUp.CustomPopupPlacementCallback; 

        _menuItemPopUp.Placement = PlacementMode.Custom; 
        _menuItemPopUp.CustomPopupPlacementCallback = CustomPopupPlacementCallback; 
       } 
      } 

      private void AssociatedObject_Loaded(object sender, RoutedEventArgs e) 
      { 
       AssociatedObject.Loaded -= AssociatedObject_Loaded; 

       if (_menuItemPopUp != null) 
        return; 

       _menuItemPopUp = VisualTreeHelpers.FindFirstChild<Popup>(AssociatedObject) as Popup; 

       if (_menuItemPopUp == null) 
        return; 

       _previousPlacementMode = _menuItemPopUp.Placement; 
       _previousCallback = _menuItemPopUp.CustomPopupPlacementCallback; 

       _menuItemPopUp.Placement = PlacementMode.Custom; 
       _menuItemPopUp.CustomPopupPlacementCallback = CustomPopupPlacementCallback; 
      } 

      protected override void OnDetaching() 
      { 
       base.OnDetaching(); 

       if(AssociatedObject != null) 
       { 
        AssociatedObject.Loaded -= AssociatedObject_Loaded; 
       } 

       if (_menuItemPopUp != null) 
       { 
        _menuItemPopUp.Placement = _previousPlacementMode; 
        _menuItemPopUp.CustomPopupPlacementCallback = _previousCallback; 

        _previousCallback = null; 
       } 
      } 

      private CustomPopupPlacement[] CustomPopupPlacementCallback(Size popupSize, Size targetSize, Point offset) 
      { 
       var rightPlacedPopupX = offset.X; 
       var rightPlacedPopupY = offset.Y + (AssociatedObject.ActualHeight * YourLogic); 

       return new[] 
       { 
        new CustomPopupPlacement(new Point(rightPlacedPopupX, rightPlacedPopupY), PopupPrimaryAxis.Horizontal), 
       }; 
      } 
     }