gpt4 book ai didi

c# - 在 WPF 中拖放 TabItems?

转载 作者:太空宇宙 更新时间:2023-11-03 10:39:02 25 4
gpt4 key购买 nike

所以我找到了这个回答的问题: Is it possible to rearrange tab items in tab control in wpf?

使用该线程中的信息,我在我的应用程序中进行了所有设置:

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

An exception of type 'System.NullReferenceException' occurred in Scoreboard Assistant.exe but was not handled in user code

Additional information: Object reference not set to an instance of an object.

当我点击继续时,出现以下错误

        DragDrop.DoDragDrop(tabItem, tabItem, DragDropEffects.All);

An unhandled exception of type 'System.NullReferenceException' occurred in PresentationCore.dll

Additional information: Object reference not set to an instance of an object.

然后程序就死了。我做错了什么?

* 编辑 *

好的,我知道问题出在哪里了;我只需要帮助修复它。如果按如下方式创建选项卡项,则它可以正常工作:

<TabItem Header="TabItem"/>

但是,我的标签创建如下:

<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<Image Source="images/text.png" />
<TextBlock Text="Text"/>
</StackPanel>
</TabItem.Header>
</TabItem>

如您所见,我正在使用堆栈面板在选项卡标题中显示一个图标。问题似乎是,当我拖放面板时,它不是将 e.Source 读取为 tabitem,而是读取 tabitem 的堆栈面板中的文本 block 。我该如何解决这个问题?

最佳答案

由于 TabItem header 的可视树可能相当复杂,您不能保证放置目标将是 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
}
}
}

关于c# - 在 WPF 中拖放 TabItems?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26174695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com