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

时间:2018-09-19 18:22:51

标签: c# wpf mvvm

我是第一次使用ContentControl。在只有一个视图模型之前,我的选择发生了变化,只有一个视图模型可以正常工作。现在,我已经添加了ContentControl以在2个视图模型之间交替,除了<i:Interaction.Triggers>命令之外,所有命令均有效。我在ExportSelection方法中放置了一个断点,并且断点有时仅会被命中,通常是在选项卡之间切换时出现的(当我在列表视图中选择一个项目时,断点会被命中,因此选项卡的更改不会影响它吗?)。

我的configviewmodel中有两个按钮,可以在更改选择的情况下在两个视图模型之间切换。

第一个按钮设置ExportsViewModel:

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

第二个视图模型:

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

ListView:

<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>

缩小的ViewModel:

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 个答案:

没有答案
相关问题