Я использую ContentControl
в первый раз.Раньше у меня была только одна модель представления, и мой выбор изменился, и он работал нормально только с одной моделью представления.Теперь, когда я добавил ContentControl
для чередования между двумя моделями представления, все команды работают, кроме команды <i:Interaction.Triggers>
.Я установил точки останова в методе ExportSelection, и точка останова срабатывает только иногда, обычно, когда я переключаюсь между вкладками (точка останова должна попадать, когда я выбираю элемент в списке, чтобы изменение вкладки не влияло на него?).
У меня есть две кнопки в моей configviewmodel, которые переключаются между двумя viewmodels с измененным выбором.
Первая кнопка устанавливает ExportsViewModel:
_MainWindow.MainWindowContent.Content = ManifestViewModel.This;
_MainWindow.MainWindowExportContent.Content = ExportViewModel.This;
Вторая viewmodel:
_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>
Минимизированная модель представления:
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();
}
}
}
}