【问题标题】:AutoCompleteBox Don't SelectedItem on Down/Up KeyAutoCompleteBox Don't SelectedItem on Down/Up Key
【发布时间】:2012-05-30 18:51:43
【问题描述】:

我在 MVVM 中使用 AutoCompleteBox,并且我只想在用户单击项目或用户按 Enter 时执行某些操作。

但是现在当我使用键盘上的 down\Up 键时, selectedItem 属性会发生变化...

我的控件:

<Controls:AutoCompleteBox ItemsSource="{Binding IndicationDtos, Mode=TwoWay}" 
                              Width="100" SelectedItem="{Binding IndicationSelected, Mode=TwoWay}" 
                              ValueMemberPath="Diagnosis" Text="{Binding Criteria, Mode=TwoWay}" MinimumPopulateDelay="250"/>

如何使属性“SelectedItem”仅在 Enter 或单击时分配?

如果您有任何问题...

非常感谢

【问题讨论】:

    标签: mvvm autocomplete selecteditem


    【解决方案1】:

    在您的 SelectedItem 绑定中,您可以使用:

    SelectedItem="{Binding IndicationSelected, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"  
    

    这样选择的项目只有在你专注于其他事情时才会改变

    【讨论】:

    • 谢谢,但我没有 UpdateSourceTrigger=LostFocus,我使用 Silverlight 是为了这个?其他想法?
    • 啊不,对于silverlight,您需要一些移动定制的东西。看到这个:stackoverflow.com/questions/6145994/…
    • 我无法理解如何使用 LostFocus 解决我的问题?
    • 它会延迟您选择的项目的更改,直到您点击其他地方。
    【解决方案2】:

    我找到了创建新课程的解决方案。

    像这样:

        public class AutoCompleteBoxEx : AutoCompleteBox
    {
                public static readonly DependencyProperty SelectionBoxItemProperty =
            DependencyProperty.Register(
            "SelectionBoxItem",
            typeof(object),
            typeof(AutoCompleteBox),
            new PropertyMetadata(OnSelectionBoxItemPropertyChanged));
    
        public object SelectionBoxItem
        {
            get
            {
                return GetValue(SelectionBoxItemProperty);
            }
    
            set
            {
                SetValue(SelectionBoxItemProperty, value);
            }
        }
    
        protected override void OnDropDownClosing(RoutedPropertyChangingEventArgs<bool> e)
        {
            base.OnDropDownClosing(e);
            SelectionBoxItem = SelectionAdapter.SelectedItem;
        }
    
        private static void OnSelectionBoxItemPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      相关资源
      最近更新 更多