2014-10-03 2 views
0

그래서 난이 질문에 대답을 발견 : 스레드의 정보를 사용하여 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을 읽는 것입니다. 어떻게 해결할 수 있을까요?

+0

은 물론,'e.Source'는'TabItem' 없습니다. ** 결과의 null-checking을 따르지 않고'as '연산자를 사용하지 마라. – Dennis

+0

예, 정확합니다 ... 오류가 더 확대되는 게시물을 업데이트하고 있습니다. –

답변

1

TabItem 헤더의 시각적 트리가 다소 복잡 할 수 있으므로 드롭 대상이 TabItem 인스턴스 (코드에서 발생하는 것)가 될 것이라고 보증 할 수 없습니다.

하지만 시각적 트리를 탐험을 통해 TabItem을 찾을 수는 :

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_Drop(object sender, DragEventArgs e) 
{ 
    var tabItemTarget = GetTargetTabItem(e.OriginalSource); 
    if (tabItemTarget != null) 
    { 
     var tabItemSource = (TabItem)e.Data.GetData(typeof(TabItem)); 
     if (tabItemTarget != tabItemSource) 
     { 
      // the rest of your code 
     } 
    } 
} 
+0

잘 했어! 감사 –