Делаю перетаскивание вкладок,как в браузере.Выдает ошибку Операция недопустима, когда ItemsSource используется. Вместо этого получите доступ и измените элементы с помощью ItemsControl.ItemsSource в строке
int sourceIndex = TabItem.Items.IndexOf(tabItemSource);
Подскажите,что я пропустил.
вот код с#
private TabItem GetTargetTabItem(object originalSource)
{
var current = originalSource as DependencyObject;
while (current != null)
{
var tabItem = current as TabItem;
if (tabItem != null)
{
return tabItem;
}
current = VisualTreeHelper.GetParent(current);
}
return null;
}
private void TabItem_PreviewMouseMove(object sender, MouseEventArgs e)
{
var tabItem = e.Source as TabItem;
if (tabItem == null)
return;
if (Mouse.PrimaryDevice.LeftButton == MouseButtonState.Pressed)
{
DragDrop.DoDragDrop(tabItem, tabItem, DragDropEffects.All);
}
}
private void TabItem_Drop(object sender, DragEventArgs e)
{
var tabItemTarget = GetTargetTabItem(e.OriginalSource);
if (tabItemTarget != null)
{
var tabItemSource = (TabItem)e.Data.GetData(typeof(TabItem));
if (tabItemTarget != tabItemSource)
{
int sourceIndex = TabItem.Items.IndexOf(tabItemSource);
int targetIndex = TabItem.Items.IndexOf(tabItemTarget);
TabItem.Items.Remove(tabItemSource);
TabItem.Items.Insert(targetIndex, tabItemSource);
TabItem.Items.Remove(tabItemTarget);
TabItem.Items.Insert(sourceIndex, tabItemTarget);
TabItem.SelectedIndex = targetIndex;
}
и xml
<Window x:Class="WPFDynamicTab.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Dynamic Tab" Height="300" Width="527" WindowStartupLocation="CenterScreen">
<TabControl>
<TabControl Name="TabItem" ItemsSource="{Binding}" SelectionChanged="tabDynamic_SelectionChanged">
<TabControl.Resources>
<Style TargetType="TabItem" >
<Setter Property="AllowDrop" Value="True"/>
<EventSetter Event="PreviewMouseMove" Handler="TabItem_PreviewMouseMove"/>
<EventSetter Event="Drop" Handler="TabItem_Drop"/>
</Style>
<DataTemplate x:Key="TabHeader" DataType="TabItem">
<DockPanel>
<Button Name="btnDelete" DockPanel.Dock="Right" Margin="5,0,0,0" Padding="0" Click="btnDelete_Click" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Name}">
<!--<Image Source="/delete.gif" Height="11" Width="11"></Image>-->
</Button>
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=TabItem }, Path=Header}" />
</DockPanel>
</DataTemplate>
</TabControl.Resources>
</TabControl>
</TabControl>
Современные инструменты для криптотрейдинга: как технологии помогают принимать решения
Апостиль в Лос-Анджелесе без лишних нервов и бумажной волокиты
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости