c# - TabControl 事件 SelectionChanged 在同一项目上更改时执行了两次
问题描述
在我的程序中,我动态创建 TabItems,并且可以创建一些相同的项目。问题是当我从一个切换到第二个相同的项目时。在这一刻事件中:
private void TabCards_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.Source is TabControl)
{
...
}
}
跑2次。我可以在短时间内给秒表和阻止运行此事件两次。但我不认为这是一个好的解决方案。
也许有人知道我能做什么?
编辑
这就是我添加项目的方式:
private void AddTabItemForCard(string cardName)
{
var style = (Style)resources.FindResource("Style" + cardName);
var tabItem = new TabItem
{
Style = style,
Header = cardName
};
TabCards.Items.Add(tabItem);
}
这是 xaml 中的 TabControl:
<TabControl x:Name="TabCards" Grid.Column="1" SelectionChanged="TabCards_SelectionChanged" >
</TabControl>
和风格:
<Style x:Key="StyleWej" TargetType="{x:Type TabItem}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="AllowDrop" Value="True"/>
<EventSetter Event="PreviewMouseMove" Handler="TabItem_PreviewMouseMove"/>
<EventSetter Event="Drop" Handler="TabItem_Drop"/>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Grid>
<TextBlock TextWrapping="Wrap" Text="Adres(hex)" FontSize="15" Margin="10,10,396,444"/>
<ComboBox x:Name="AddressWej" HorizontalAlignment="Left" Margin="134,11,0,0" VerticalAlignment="Top" Width="120">
<ComboBoxItem>30</ComboBoxItem>
<ComboBoxItem>31</ComboBoxItem>
<ComboBoxItem>32</ComboBoxItem>
</ComboBox>
<TextBlock TextWrapping="Wrap" Text="Wejscie 1:" FontSize="15" Margin="10,126,396,337"/>
<TextBlock TextWrapping="Wrap" Text="Wejscie 2:" FontSize="15" Margin="10,156,396,307"/>
<TextBox TextWrapping="Wrap" Text="Wejscie1" Width="120" Margin="134,129,231,331"/>
<TextBox TextWrapping="Wrap" Text="Wejscie2" Width="120" Margin="134,157,231,302"/>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
事件:
private void TabItem_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (!(e.Source is TabItem tabItem))
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))
{
var tabControl = tabItemTarget.Parent as TabControl;
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);
}
}
在 TabCards_SelectionChanged 我用 TabItem 检查它并运行:
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject rootObject) where T : DependencyObject
{
if (rootObject != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(rootObject); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(rootObject, i);
if (child != null && child is T)
yield return (T)child;
foreach (T childOfChild in FindVisualChildren<T>(child))
yield return childOfChild;
}
}
}
从 TabItem 中查找所有 TextBox
解决方案
推荐阅读
- apache - 具有身份验证表单的 Apache 反向代理未将凭据传递给代理服务器
- python - 为什么需要“预测空间”?
- jquery - jQuery - 使用变量通过href值查找锚点
- angular - 通过 angular-cli 生成的 Angular 组件的自定义项目级模板
- hibernate - 没有直接连接的休眠连接查询
- c# - 当我从另一个表单调用方法时,控件不会更改颜色或文本
- google-cloud-nl - 我们可以使用 Google NLP API 比较两个不同字符串的相似性吗?
- ios - GoogleSignin - 不允许在解除分配时尝试加载视图控制器的视图,这可能会导致未定义的行为
- c++ - C++ 的不同行为与公共和私有范围的默认值
- angularjs - AngularJS 中的基本数组循环