我正在(主要)使用MVVM的WPF应用程序上工作。我的问题是我有一个标签控件(它实际上是一个嵌套的标签控件),表现得很奇怪。在冗长的后台处理期间(这是我想要的行为!)禁用父控件,但是当后台内容完成并且父级重新启用时,其中一个嵌套选项卡保持禁用状态。并且违规选项卡会根据在后台操作之前选择的选项进行更改;有时第二个选项卡将保持禁用状态,有时第三个选项卡将保持禁用状态。
我实际上并没有触及" IsEnabled"单个选项卡的属性,通过数据绑定或通过代码隐藏。只有父控件(在重要的情况下是一个网格)被禁用。
我很欣赏任何调试这个的策略;什么原因会导致一个标签与其他标签区别对待?
实际上不需要后台处理来重现问题。它也可以通过在代码隐藏中禁用和重新启用父网格来实现:
private void test1_Click(object sender, RoutedEventArgs e)
{
myGrid.IsEnabled = false;
}
private void test2_Click(object sender, RoutedEventArgs e)
{
myGrid.IsEnabled = true;
}
编辑:我可能已将问题缩小到以下样式。如果我通过添加一个键暂时禁用该样式,那么问题就会消失;但我当然希望保持标签的视觉外观:
<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid Height="{Binding Path=HeaderHeight, ElementName=uc}" VerticalAlignment="Bottom">
<Border Name="Border"
Background="{StaticResource TabItem_BackgroundBrush_Unselected}"
BorderBrush="{StaticResource TabItem_BorderBrush_Selected}"
Margin="{StaticResource TabItemMargin_Selected}"
BorderThickness="2,1,1,0"
CornerRadius="3,3,0,0"
>
<Border BorderBrush="{StaticResource TabItem_BorderBrush_Selected}"
BorderThickness="0,0,0,1"
Margin="0"
Name="BottomBorder"
>
<ContentPresenter x:Name="ContentSite"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header"
Margin="7,2,12,2"
RecognizesAccessKey="True"
/>
</Border>
</Border>
</Grid>
<ControlTemplate.Triggers>
<!-- TabItem appearance when NOT selected -->
<Trigger Property="IsSelected" Value="False">
<Setter Property="Panel.ZIndex" Value="90" />
<Setter TargetName="Border" Property="BorderBrush"
Value="{StaticResource TabItem_Border_Unselected}" />
<Setter Property="Foreground"
Value="{StaticResource TabItem_TextBrush_Unselected}" />
<Setter TargetName="Border" Property="Margin"
Value="{StaticResource TabItemMargin_Base}" />
</Trigger>
<!-- TabItem appearance when disabled -->
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Panel.ZIndex" Value="80" />
<Setter TargetName="Border" Property="BorderBrush"
Value="{StaticResource TabItem_DisabledBorderBrush}" />
<Setter TargetName="Border" Property="Background"
Value="{StaticResource TabItem_BackgroundBrush_Disabled}" />
<Setter Property="Foreground"
Value="{StaticResource TabItem_TextBrush_Disabled}" />
</Trigger>
<!-- TabItem appearance when hovering -->
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Border.IsMouseOver" Value="True" />
<Condition Property="IsSelected" Value="False" />
</MultiTrigger.Conditions>
<Setter Property="Panel.ZIndex" Value="99" />
<Setter Property="Foreground" Value="{StaticResource TabItem_TextBrush_Hover}" />
<Setter TargetName="Border" Property="BorderBrush"
Value="{StaticResource TabItem_HoverBorderBrush}" />
<Setter TargetName="Border" Property="BorderThickness"
Value="2,1,1,1" />
<Setter TargetName="BottomBorder" Property="BorderThickness"
Value="0,0,0,0" />
<Setter TargetName="Border" Property="Background"
Value="{StaticResource TabItem_HoverBackgroundBrush}" />
<Setter TargetName="Border" Property="Margin"
Value="{StaticResource TabItemMargin_Hover}" />
</MultiTrigger>
<!-- TabItem appearance when active/selected -->
<Trigger Property="IsSelected" Value="True">
<Setter Property="Panel.ZIndex" Value="100" />
<Setter TargetName="Border" Property="BorderBrush"
Value="{StaticResource TabItem_BorderBrush_Selected}" />
<Setter TargetName="Border" Property="Background"
Value="{StaticResource TabItem_BackgroundBrush_Selected}" />
<Setter TargetName="BottomBorder" Property="BorderThickness"
Value="0,0,0,0" />
<Setter TargetName="Border" Property="BorderThickness"
Value="1,1,1,0" />
<Setter Property="Foreground" Value="{StaticResource TabItem_TextBrush_Selected}" />
<Setter TargetName="Border" Property="Margin"
Value="{StaticResource TabItemMargin_Selected}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>