【问题标题】:How to bring back the focus to combo box in wpf?如何将焦点带回wpf中的组合框?
【发布时间】:2021-10-27 17:28:21
【问题描述】:

我正在尝试使用向上/向下箭头键更改组合框的项目。

场景是我的组合框根据某些条件禁用,一旦满足条件,它就会启用。

问题是组合框启用后焦点丢失了。

这是我的 View.xaml 代码:

    <StackPanel Margin="2.5">
        <Label Content="{x:Static local:StringResources.LBL_FILL_LOC}" Target=" 
        {Binding ElementName=CboFillLoc}"/>
        <ComboBox x:Name="CboFillLoc" IsEnabled="{Binding IsComboEnabled}"
         ItemsSource="{Binding Locations}" SelectedItem="{Binding 
         SelectedLocation, Mode=TwoWay}" Padding="2.5"/>
    </StackPanel>

这里是启用组合框的地方 (ViewModel.cs)


    private async void FetchItems()
        {
            try
            {
                 do something
            }
            catch
            {
        
            }
            finally
            {
                 SearchStatus = StringResources.LBL_READY;
                 ItemsLoading = false;
                 IsComboEnabled = true;
            }
        
        }
    

这是我在 view.xaml 中尝试过的:


    <StackPanel Margin="2.5">
        <Label Content="{x:Static local:StringResources.LBL_FILL_LOC}" Target=" 
        {Binding ElementName=CboFillLoc}"/>
        <ComboBox x:Name="CboFillLoc" IsEnabled="{Binding IsComboEnabled}"
         Focusable="{Binding IsFocusable}"
         ItemsSource="{Binding Locations}" SelectedItem="{Binding 
         SelectedLocation, Mode=TwoWay}" Padding="2.5"/>
    </StackPanel>

在 ViewModel.cs 中我声明了这一点并将其绑定到可聚焦属性

     public bool IsFocused
            {
                get => _isFocused;
                set
                {
                    if (_isFocused == value) return;
                    _isFocused = value;
                    OnPropertyChanged("IsFocused");
                }
            }
    

在这里我试图将焦点放在组合框上


    private async void FetchItems()
        {
            try
            {
                 do something
            }
            catch
            {
        
            }
            finally
            {
                 SearchStatus = StringResources.LBL_READY;
                 ItemsLoading = false;
                 IsComboEnabled = true;
                 IsFocusable = true;
            }
        
        }
    

有人能指出我在这里做错了什么吗?

【问题讨论】:

  • 请发布代码而不是图片。
  • 这就是我在我的问题上获得 -ve 票的原因吗?我在stackoverflow上不是很频繁,所以不知道这些。
  • 一般来说,如果您的代码失败,您应该提供一个最小的运行示例来重现该问题。不要指望人们坐下来从你的图像中输入代码,以防他们想要测试它。请参阅我的答案:您可以复制并粘贴它。还是我应该发布代码图像? How do I ask a good question?, How to create a Minimal, Reproducible Example

标签: c# wpf xaml model-view-controller model


【解决方案1】:

您可以使用触发器而不是绑定:

<ComboBox x:Name="ComboBox">
  <ComboBox.Style>
    <Style TargetType="ComboBox">
      <Style.Triggers>
        <Trigger Property="IsEnabled" Value="True">
          <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=ComboBox}" />
        </Trigger>
        <DataTrigger Binding="{Binding IsComboEnabled}" Value="False">
          <Setter Property="IsEnabled" Value="False" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </ComboBox.Style>
</ComboBox>

旁注:Focusable 的确切含义是:该元素是可聚焦的。这并不意味着专注。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 2016-02-10
    相关资源
    最近更新 更多