그래서 난이 질문에 대답을 발견 : 스레드의 정보를 사용하여 Is it possible to rearrange tab items in tab control in wpf?WPF에서 끌어서 놓기 TabItem?
을, 난 내 응용 프로그램에서 모든 설정 :
<TabControl x:Name="tabControl">
<TabControl.Resources>
<Style TargetType="TabItem">
<Setter Property="AllowDrop" Value="True"/>
<EventSetter Event="PreviewMouseMove" Handler="TabItem_Drag"/>
<EventSetter Event="Drop" Handler="TabItem_Drop"/>
</Style>
</TabControl.Resources>
</TabControl>
그리고 코드 :
private void TabItem_Drag(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 = e.Source as TabItem;
var tabItemSource = e.Data.GetData(typeof(TabItem)) as TabItem;
if (!tabItemTarget.Equals(tabItemSource))
{
int sourceIndex = tabControl.Items.IndexOf(tabItemSource);
int targetIndex = tabControl.Items.IndexOf(tabItemTarget);
tabControl.Items.Remove(tabItemSource);
tabControl.Items.Insert(targetIndex, tabItemSource);
tabControl.Items.Remove(tabItemTarget);
tabControl.Items.Insert(sourceIndex, tabItemTarget);
tabControl.SelectedIndex = targetIndex;
}
}
탭을 놓으면 다음과 같은 오류가 발생합니다.
if (!tabItemTarget.Equals(tabItemSource))
,
, 나는'System.NullReferenceException'형식의 예외 스코어 Assistant.exe 발생하지만
가격 정보, 사용자 코드에 처리되지 : 개체 참조가 개체의 인스턴스로 설정되지. 개체 참조 : 나는 계속을 클릭
DragDrop.DoDragDrop(tabItem, tabItem, DragDropEffects.All);
처리되지 않은 'System.NullReferenceException'형식의 예외에 다음과 같은 오류가 PresentationCore.dll
추가 정보 발생 객체의 인스턴스로 설정되지 않습니다.
그리고 나서 프로그램이 종료됩니다. 내가 도대체 뭘 잘못하고있는 겁니까?
은 EDIT *
좋아, 문제가 무엇인지 파악 *; 나는 그것을 고치는 데 도움이 필요합니다. 탭 항목이 생성되는 경우가 완벽하게 잘 작동 다음과 같이
<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<Image Source="images/text.png" />
<TextBlock Text="Text"/>
</StackPanel>
</TabItem.Header>
</TabItem>
당신이 볼 수 있듯이, 나는이를 위해 StackPanel을 사용하고 있습니다 : 그러나
<TabItem Header="TabItem"/>
다음과 같이 내 탭이 생성되고있다 아이콘이 탭 머리글에 있습니다. 문제는 내가 끌어다 놓을 때, tabiteem으로 읽혀지는 e.Source
대신에 tabitem의 스택 판넬 내에서 textblock을 읽는 것입니다. 어떻게 해결할 수 있을까요?
은 물론,'e.Source'는'TabItem' 없습니다. ** 결과의 null-checking을 따르지 않고'as '연산자를 사용하지 마라. – Dennis
예, 정확합니다 ... 오류가 더 확대되는 게시물을 업데이트하고 있습니다. –