ItemsControl 中的子控件被禁用,但我不知道是什么禁用了它们

Child controls in an ItemsControl are being disabled, but I can't figure out what's disabling them

我正在尝试在 WPF 中创建浏览器风格的选项卡界面。我把它放在一起并工作了一段时间,但现在所有使选项卡起作用的按钮都被禁用了。在实时可视化树中,它表示覆盖并显示强制设置 IsEnabled = false。 None 父项已禁用。

我尝试过一些东西。我试着明确说明一切都已启用。这没有帮助。接下来我尝试的是清除所有内容并使用普通按钮。这行得通,所以我认为问题出在我的风格上。

这行不通

<ItemsControl ItemsSource="{Binding PageViewModels}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="70,15,0,0">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate >
            <Button Background="{x:Null}" Height="24" MinWidth="140"  Padding="0,0,0,0" BorderThickness="0" Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor} }" CommandParameter="{Binding }">
                <Border MinWidth="140" Height="24" >
                    <Border.Style>
                        <Style TargetType="Border">
                            <Style.Triggers>
                                <DataTrigger Value="True">
                                   <DataTrigger.Binding>
                                        <MultiBinding Converter="{StaticResource IsEqual}">
                                            <Binding Path=".ID"/>
                                            <Binding Path="DataContext.CurrentPageViewModel.ID" RelativeSource="{RelativeSource AncestorType=Window, Mode=FindAncestor}"/>
                                        </MultiBinding>
                                    </DataTrigger.Binding>
                                    <Setter Property="Background" Value="{StaticResource LIGHT_Main_100}"/>
                                    <Setter Property="BorderThickness" Value="0,0,0,3"/>
                                    <Setter Property="BorderBrush" Value="{StaticResource DARK_CYAN_Secondary_100}"/>
                                </DataTrigger>
                            </Style.Triggers>
                            <Setter Property="Background" Value="{StaticResource LIGHT_Main_85}"/>
                            <Setter Property="BorderThickness" Value="0,0,.5,.5"/>
                            <Setter Property="BorderBrush" Value="{StaticResource DARK_Faded }"/>
                        </Style>
                    </Border.Style>
                    <DockPanel VerticalAlignment="Stretch" Margin="10,0,0,0">
                        <Label DockPanel.Dock="Left" Content="{Binding Name}" FontSize="15" Padding="0" Loaded="Label_Loaded" >
                            <Label.Style>
                                <Style TargetType="Label">
                                    <Style.Triggers>
                                        <DataTrigger Value="False">
                                            <DataTrigger.Binding>
                                                  <MultiBinding Converter="{StaticResource IsEqual}">
                                                    <Binding Path=".ID"/>
                                                    <Binding Path="DataContext.CurrentPageViewModel.ID" RelativeSource="{RelativeSource AncestorType=Window, Mode=FindAncestor}"/>
                                                </MultiBinding>
                                             </DataTrigger.Binding>
                                                    <Setter Property="Foreground" Value="{StaticResource DARK_Faded}"/>
                                                    <Setter Property="FontWeight" Value="ExtraLight"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                            <Setter Property="FontWeight" Value="Bold"/>
                                        </Style>
                                    </Label.Style>
                                </Label>
                                <Button DockPanel.Dock="Right" Background="{x:Null}" Content="&#10539;" Height="24" Width="28" FontSize="15" Padding="0,-6,0,0" HorizontalAlignment="Right" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" BorderThickness="0" Command="{Binding ClosePage }" CommandParameter="{Binding}">
                                    <Button.Style>
                                        <Style TargetType="Button">
                                            <Style.Triggers>
                                                <DataTrigger Value="True">
                                                    <DataTrigger.Binding>
                                                        <MultiBinding Converter="{StaticResource IsEqual}">
                                                            <Binding Path=".ID"/>
                                                            <Binding Path="DataContext.CurrentPageViewModel.ID" RelativeSource="{RelativeSource AncestorType=Window, Mode=FindAncestor}"/>
                                                        </MultiBinding>
                                                </DataTrigger.Binding>
                                                <Setter Property="Visibility" Value="Visible"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                            <Setter Property="Visibility" Value="Hidden"/>
                                        </Style>
                                    </Button.Style>
                                </Button>
                            </DockPanel>
                        </Border>
                    </Button>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

这行得通

<ItemsControl ItemsSource="{Binding PageViewModels}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="70,15,0,0">
    <ItemsControl.ItemsPanel>
         <ItemsPanelTemplate>
             <StackPanel Orientation="Horizontal"/>
         </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
         <DataTemplate >
             <Button Content="Test"/>
         </DataTemplate>
     </ItemsControl.ItemTemplate>
 </ItemsControl>

编辑

这可能是导致我出现问题的原因。前一段时间,我从网上的一个例子中复制了这段代码,但并不理解,但它确实有效。我认为 canExecute 是错误的,因为我没有发送字符串参数,而是一个对象。感谢您的帮助@Neil

public ICommand ChangePageCommand
{
    get
    {
        if (_changePageCommand == null)
        {
            _changePageCommand = new RelayCommand(
                p => ChangeViewModel((string) p),
                p => p is string);
        }

        return _changePageCommand;
    }
}

public class RelayCommand : ICommand
{
    #region Fields

    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;

    #endregion // Fields

    #region Constructors

    /// <summary>
    /// Creates a new command that can always execute.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    public RelayCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    /// <summary>
    /// Creates a new command.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    /// <param name="canExecute">The execution status logic.</param>
    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;
    }

    #endregion // Constructors

    #region ICommand Members
    public bool CanExecute(object parameters)
    {
        return _canExecute == null ? true : _canExecute(parameters);
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameters)
    {
        _execute(parameters);
    }

    #endregion // ICommand Members
}

您的 ChangePageCommand 命令的 CanExecute 可能返回 false。要检查,只需删除命令绑定并查看按钮是否仍处于禁用状态。

Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor} }"