2011-03-17 5 views
3

콤보 박스를 편집 가능하게하고 드롭 다운 상태를 유지하고 싶습니다. 이러한 특성을 가진 순간 WPF Combobox의 드롭 다운을 만드는 방법 & 배치하기

이 설정되었습니다 : 입력 텍스트 상자 또는 초점의 사용자 클릭이 다른 컨트롤로 변경 될 때마다

IsEditable="True" IsDropDownOpen="True" StaysOpenOnEdit="True" 

에서, dorpdown이 닫힙니다. 그래서 템플릿을 업데이트했습니다 (WPF Theme : BureauBlue에 포함 된 템플릿) PopupIsOpen="true"이 특별한 경우에는 드롭 다운이 열려 있지만 지금은 사용자가 &을 드래그하면 드롭 다운이 이 아닌 위치를 업데이트합니다. 자동으로 은 이전 위치에으로 있습니다.

열린 상태에서 자동으로 위치를 업데이트하도록하려면 어떻게합니까??

답변

7

당신은 트릭은 여기에 설명 사용할 수 있습니다

: 여기
/// <summary> 
/// A behavior that forces the associated popup to update its position when the <see cref="Popup.PlacementTarget"/> 
/// location has changed. 
/// </summary> 
public class AutoRepositionPopupBehavior : Behavior<Popup> { 
    public Point StartPoint = new Point(0, 0); 
    public Point EndPoint = new Point(0, 0); 

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

     if (AssociatedObject.PlacementTarget != null) { 
      AssociatedObject.PlacementTarget.LayoutUpdated += OnPopupTargetLayoutUpdated; 
     } 
    } 

    void OnPopupTargetLayoutUpdated(object sender, EventArgs e) { 
     if (AssociatedObject.IsOpen) { 
      ResetPopUp(); 
     } 
    } 

    public void ResetPopUp() { 
     // The following trick that forces the popup to change it's position was taken from here: 
     // http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/27950e73-0007-4e0b-9f00-568d2db1d979 
     Random random = new Random(); 
     AssociatedObject.PlacementRectangle = new Rect(new Point(random.NextDouble()/1000, 0), new Size(75, 25)); 
    } 
} 

이 동작을 적용하는 방법을 예입니다

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/27950e73-0007-4e0b-9f00-568d2db1d979 내가 쉽게 어떤 팝업으로 사용할 수 있도록하는 Blend behavior을 생성

<Popup ...> 
    <i:Interaction.Behaviors> 
     <Behaviors:AutoRepositionPopupBehavior /> 
    </i:Interaction.Behaviors> 
</Popup> 
+0

답변을 주셔서 감사합니다.하지만 동작을 구현했지만 'OnPopupTargetLayoutUpdated'가 실행되지 않는 경우 (예 : 창을 이동할 때), 모든 sugges tions? – Bolu

+0

거기서 초보자를 위해서, 당신은 당신의 팝업의 배치 대상이 작동하도록 설정해야합니다. 감사합니다 PG, 이것은 훌륭하게 작동했습니다. –

관련 문제