簡體   English   中英

WPF:如何關閉<Popup>當在滾動查看器鼠標滾輪動作之外時

[英]WPF: How to close <Popup> when outside scrollviewer mousewheel action

當外部滾動查看器鼠標滾輪改變時,我想要一個關於關閉彈出控件的解決方案。

<ScrollViewer>
    ....
    <Grid>
        <TextBox x:Name="PART_Text"/>
        <Popup IsOpen="{Binding IsDropDown}" StayOpen="False" 
               PlacementTarget{BInding ElementName=PART_Text">
            <Border>...</Border>
        </Popup>
    </Grid>
</ScrollViewer>

我想讓彈出窗口自動關閉,當滾輪移動時,而不是鼠標點擊

對未來的提示:如果你讓人們更容易回答問題,你會更幸運地得到回答。 您的代碼無法編譯的原因是 1) 您的StaysOpen屬性拼寫錯誤,2) 您的PlacementTarget設置沒有賦值運算符和開引號,3) 您的Binding關鍵字大小寫錯誤以及 4) setter 沒有有一個結束括號。

要回答您的問題,您需要做的就是為PreviewMouseWheel事件添加一個命令處理程序。 攔截事件的確切位置取決於您想要的行為; 如果您希望它在應用程序中的任何控件具有焦點時發生,則將其添加到 MainWindow,否則將其添加到您的 ScrollViewer:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

<ScrollViewer>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="PreviewMouseWheel">
            <i:InvokeCommandAction Command="{Binding PreviewMouseWheelCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>

然后在您的視圖模型中為它​​添加一個命令處理程序:

    private ICommand _PreviewMouseWheelCommand;
    public ICommand PreviewMouseWheelCommand => this._PreviewMouseWheelCommand ?? (this._PreviewMouseWheelCommand = new RelayCommand(OnPreviewMouseWheel));

    private void OnPreviewMouseWheel()
    {
        this.IsDropDown = false;
    }

只要您的IsDropDown屬性支持 INPC,每當發生PreviewMouseWheel事件時,彈出窗口就會消失。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM