首页 文章

WPF ListBox多次选择不会在模型上引发SelectedItem

提问于
浏览
1

我的ListBox有问题我已经将ListBox选择模式设置为Multiple问题是Selected Item只触发一次,当我选择其他项时,它不会更改视图模型中的SelectedItem . 我确定它选择它们是因为我在选中它们时将属性绑定为true,但所选项目不会更新 . 例如:假设我有以下列表框:ABC选择A - > ViewModel将selectedItem更新为A选择B - > ViewModel不更新SelectedItem但我可以看到B被选中当我取消选择A时, ViewModel将SelectedItem更新为null有人已经遇到过这个问题吗?我的主要目标是将我的SelectedItem更新保持为我选择的更新他是我的视图代码:

<ListBox HorizontalAlignment="Center"  ItemsSource="{Binding AvailableDagrVMEs}"
                     SelectedItem="{Binding SelectedDagrVME}"
                     SelectionMode="Multiple"
                     VirtualizingStackPanel.IsVirtualizing="False"
                     ScrollViewer.HorizontalScrollBarVisibility="Auto" Padding="0" Margin="0" ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.PanningMode="HorizontalOnly"
                     Background="Transparent"
                     BorderThickness="0">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <UniformGrid Rows="1"/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.ItemContainerStyle>
                        <Style TargetType="{x:Type ListBoxItem}">
                            <Setter Property="IsSelected" Value="{Binding Taken, Mode=TwoWay}"/>
                        </Style>
                    </ListBox.ItemContainerStyle>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <DockPanel>
                                <Image Source="{Binding Icon , Converter={StaticResource BitmapToImageSourceConverter}}"/>
                            </DockPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

和ViewModel:

public BaseVME SelectedDagrVME
    {
        get { return selectedDagrVME; }
        set
        {
            if (selectedDagrVME != value)
            {
                Set("SelectedDagrVME", ref selectedDagrVME, value);
            }
        }
    }

我试过了:TwoWay binding / UpdateTriggerSource

1 回答

  • 1

    由于您有 Multiple 选择, SelectedItem 将始终是您选择中的第一个 . 要获取最后选择的项目,您必须注册 SelectionChanged 事件并访问 SelectedItems 属性,其中包含您选择的所有项目 . 您可以通过行为实现它,以便重用它 .

    Behavior:

    public class ListBoxSelection : Behavior<ListBox>
    {
        public static readonly DependencyProperty LastSelectedProperty = DependencyProperty.Register(nameof(LastSelected), typeof(object), typeof(ListBoxSelection), new PropertyMetadata(default(object)));
    
        public object LastSelected
        {
            get
            {
                return (object)GetValue(LastSelectedProperty);
            }
            set
            {
                SetValue(LastSelectedProperty, value);
            }
        }
    
        protected override void OnAttached()
        {
            base.OnAttached();
    
            AssociatedObject.SelectionChanged += AssociatedObject_SelectionChanged;
        }
    
        private void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
    //you can also use whatever logic if you evaluate e.RemovedItems and e.AddedItems
            if ((AssociatedObject?.SelectedItems?.Count??0)>0)
            {
                LastSelected = AssociatedObject.SelectedItems[AssociatedObject.SelectedItems.Count-1];
            }
            else
            {
                LastSelected = null;
            }
    
        }
    
        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.SelectionChanged -= AssociatedObject_SelectionChanged;
        }
    }
    

    XAML:

    xmlns:i =“clr-namespace:System.Windows.Interactivity; assembly = System.Windows.Interactivity”xmlns:b =“clr-namespace:NameSpaceWhereBahaviorDefined”

    <ListBox ...>
        <i:Interaction.Behaviors>
            <b:ListBoxSelection LastSelected = "{Binding VMSelection}" />
        </i:Interaction.Behaviors>
    </ListBox>
    

相关问题