代码之家  ›  专栏  ›  技术社区  ›  Dev

EventTrigger未在内容控件中触发选择更改事件

  •  0
  • Dev  · 技术社区  · 7 年前

    我在用 ContentControl 这是第一次。之前,我只有一个视图模型,并且我的选择发生了变化,但在只有一个视图模型的情况下,效果很好。现在我补充说 内容控制 要在两个视图模型之间切换,除了 <i:Interaction.Triggers> 命令我在ExportSelection方法中设置了一个断点,断点只会偶尔被击中,通常是在我在选项卡之间切换时(当我在listview中选择一个项目时,断点应该被击中,所以选项卡的更改不应该影响它?)。

    我的configviewmodel中有两个按钮,可在两个ViewModel之间切换,选项已更改。

    第一个按钮设置ExportsViewModel:

    _MainWindow.MainWindowContent.Content = ManifestViewModel.This;
    _MainWindow.MainWindowExportContent.Content = ExportViewModel.This;   
    

    第二个视图模型:

    _MainWindow.MainWindowContent.Content = WeKnowViewModel.This;         
    _MainWindow.MainWindowExportContent.Content = WeKnowExportViewModel.This;  
    

    列表视图:

    <ListView  x:Name="lvExport" MaxHeight="900" VirtualizingPanel.IsVirtualizing="True" ToolTip="Double click to send to Trio" VirtualizingPanel.VirtualizationMode="Recycling"  SelectionMode="Extended" AlternationCount="2" ItemsSource="{Binding CurrentExportItem, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True" Visibility="{Binding Path=IsVisibleExport, Converter={StaticResource BoolToVis} }" ItemContainerStyle="{DynamicResource stLbItem}" Height="509" VerticalAlignment="Top" >
        <ListView.View>
    
        </ListView.View>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding ExportSelectionChangedCommand}"  CommandParameter="{Binding ElementName=lvExport}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ListView>
    

    内容控制:

    <Grid Margin="435,0,0,0" Height="420" VerticalAlignment="Top" HorizontalAlignment="Left" Width="1109">
          <ContentControl Name="MainWindowExportContent" Margin="0,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"></ContentControl>
    </Grid>
    

    数据模板:

    <DataTemplate  DataType="{x:Type viewModel:ExportViewModel}">
        <views:ExportView x:Name = "ExportViewControl" Loaded = "ExportViewControlFunc" Margin="0,0,0,0" VerticalAlignment="Top" Width="auto" Height="auto" />
    </DataTemplate>
    <DataTemplate  DataType="{x:Type viewModel:WeKnowExportViewModel}">
        <views:WeKnowExportView x:Name = "WeKnowExportViewControl" Loaded = "WeKnowExportViewControlFunc" Margin="0,0,0,0" VerticalAlignment="Top" Width="auto" Height="auto" />
    </DataTemplate>
    

    缩小视图模型:

    class ExportViewModel : ViewModelBase
    {
        static ExportViewModel _this = new ExportViewModel();
        public static ExportViewModel This
        {
            get { return _this; }
        }
    
        public void Initialise()
        {
            This.ExportSelectionChangedCommand = new RelayCommand(This.ExportSelectionChanged);
        }
    
        public RelayCommand ExportSelectionChangedCommand
        {
            get;
            set;
        }
    
        void ExportSelectionChanged(object parameter)
        {
            try
            {
                //Don't let more than 6 items be selected at once, because the container only has space for 6
                ListBox listBox = parameter as ListBox;
    
                if (listBox.SelectedItems.Count > 20)
                {
                    listBox.SelectedItems.RemoveAt(listBox.SelectedItems.Count - 2);
                }
    
                //Clear out the out list of items and add the new ones so we can use them to build to a single template in viz
                This.SelectedExport = new ObservableCollection<Item2>();
                This.SelectedExport.Clear();
    
                foreach (Item2 mJ in listBox.SelectedItems)
                {
                    This.SelectedExport.Add(mJ);
                }
                if (listBox.SelectedItems.Count < 2)
                {
                    This.SelectedCount = listBox.SelectedItems.Count.ToString() + " Item selected";
                }
                else
                {
                    This.SelectedCount = listBox.SelectedItems.Count.ToString() + " Items selected";
                }
            }
            catch (Exception ex)
            {
                LogViewModel.This.WriteCurrentErrorOrWarningToXmlFile(ex.Message, "ExportSelectionChanged");
    
            }
        }
    
        private static ObservableCollection<SocialExportJSON> _ExportItems;
        public ObservableCollection<SocialExportJSON> ExportItems
        {
            get => _ExportItems;
            set
            {
                if (_ExportItems != value)
                {
                    _ExportItems = value;
                    OnPropertyChanged();
                }
            }
        }
    
        private static ObservableCollection<Item2> _CurrentExportItem = new ObservableCollection<Item2>();
        public ObservableCollection<Item2> CurrentExportItem
        {
            get => _CurrentExportItem;
            set
            {
                if (_CurrentExportItem != value)
                {
                    _CurrentExportItem = value;
                    OnPropertyChanged();
                }
            }
        }
    
        public void UpdateData(ManifestItem manifestItem, bool IsUpdated)
        private ObservableCollection<Item2> _SelectedExport;
        public ObservableCollection<Item2> SelectedExport
        {
            get => _SelectedExport;
            set
            {
                if (_SelectedExport != value)
                {
                    _SelectedExport = value;
                    OnPropertyChanged();
                }
            }
        }
    }
    
    0 回复  |  直到 7 年前