首页 > 解决方案 > WPF 在选择开始下方放置 Intellisense(如 PopUp)

问题描述

嗨,我想在文本框中键入的最后一个字符下方放置一个 WPF 弹出窗口,就像我们从 VS 中的智能感知中知道的那样。

我的实际代码看起来像这样有没有办法根据 textboxex 文本的坐标放置它?:

 <local:TextBoxAutoSuggestion Text="{Binding InputText, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                                        x:Name="SearchPathTextBox"
                                        local:FocusExtension.IsFocused="{Binding IsFocusOnInputTextBox, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                                        HorizontalAlignment="Stretch"
                                        Foreground="{DynamicResource Menu.Static.Foreground}"
                                        Background="Transparent"
                                        VerticalAlignment="Stretch"
                                        VerticalContentAlignment="Center"
                                        Height="45"
                                        Padding="5,0,30,0"
                                        Margin="0,0,0,0"
                                        CaretBrush="White"
                                        BindableSelectionStart="{Binding SelectionLengthInputText,Mode=TwoWay}">
          <i:Interaction.Triggers >
            <i:EventTrigger EventName="GotFocus">
              <i:InvokeCommandAction Command="{Binding TextBoxFocusedChangedCommand}" />
            </i:EventTrigger>
            <i:EventTrigger EventName="LostFocus">
              <i:InvokeCommandAction Command="{Binding TextBoxFocusedChangedCommand}" />
            </i:EventTrigger>
          </i:Interaction.Triggers>

          <local:TextBoxAutoSuggestion.InputBindings>
            <KeyBinding Command="{Binding EnterKeyPressedCommand}"
                        Key="Return" />
            <KeyBinding Command="{Binding TabKeyPressedCommandinTextBox}"
                        Key="Tab" />
            <KeyBinding Command="{Binding BackSpacePressedCommand}"
                        Key="Backspace" />
            <KeyBinding Command="{Binding SelectNextItemCommand}"
                        Key="Down" />
            <KeyBinding Command="{Binding SelectPreviousItemCommand}"
                        Key="Up" />

          </local:TextBoxAutoSuggestion.InputBindings>
        </local:TextBoxAutoSuggestion>
<Popup 
             Width="300"
             Margin="0,0,0,0"
             StaysOpen="False"
             Visibility="{Binding IsPopUpOpened, Converter={StaticResource BooleanToVisibilityConverter}}"
             IsOpen="{Binding IsPopUpOpened, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
             PlacementTarget="{Binding ElementName=SearchPathTextBox}"
             Placement="Bottom"
             IsEnabled="{Binding IsPopUpEnabled, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
             >
      <ListBox Margin="0,0,0,0"
               Visibility="{Binding IsPopUpOpened, Converter={StaticResource BooleanToVisibilityConverter}}"
               ItemsSource="{Binding FilteredPossibleSegments}"
               Foreground="{DynamicResource Menu.Static.Foreground}"
               Background="Transparent"
               SelectedItem="{Binding SelectedSegment,Mode=TwoWay}"
               ScrollViewer.VerticalScrollBarVisibility="Disabled"
               Height="200"
                >
      </ListBox>
      </Popup>

我现在使用的位置是“底部”。

标签: c#wpfxamlpopup

解决方案


我使用了弹出窗口的水平偏移属性并根据文本长度进行设置:

 <Popup 
             Width="300"
             Margin="0,0,0,0"
             StaysOpen="False"
             Visibility="{Binding IsPopUpOpened, Converter={StaticResource BooleanToVisibilityConverter}}"
             IsOpen="{Binding IsPopUpOpened, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
             PlacementTarget="{Binding ElementName=SearchPathTextBox}"
             Placement="RelativePoint"
             IsEnabled="{Binding IsPopUpEnabled, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
             HorizontalOffset="{Binding HorizontalPopUpPosition, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
             VerticalOffset="50"
        
             >

我对水平进行了试验,发现最好的方法是您需要在窗口中找到适合您位置的最佳选项。

   HorizontalPopUpPosition =330+ InputText.Length*5;

推荐阅读