XAML 如何禁用按钮但强制启用子按钮?

w3nuxt5m  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(176)

我想知道如何将Button的“IsEnabled”属性设置为false,然后启用子元素(在我的例子中是Combobox)。子元素似乎继承了父元素(在本例中是disabled button)的属性值,因此也禁用了它自己。我如何防止这种情况?
我曾尝试查找答案,但它们都使用WinUI3和Windows应用程序SDK不包含的“覆盖元数据”。

<Button
    x:Name="Button1"
    IsEnabled="False">
    <Button.Content>
        <ComboBox x:Name="ComboBox1" />
    </Button.Content>
</Button>
3z6pesqy

3z6pesqy1#

您可以像这样建立自订控件:

一般.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Buttons">

    <Style TargetType="local:CustomButton">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:CustomButton">
                    <Grid
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                        <Button
                            x:Name="ButtonControl"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            IsEnabled="{TemplateBinding IsButtonEnabled}" />
                        <ContentControl
                            x:Name="ContentControl"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Content="{TemplateBinding Content}"
                            IsEnabled="{TemplateBinding IsContentEnabled}" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

自定义按钮.cs

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace Buttons;

[TemplatePart(Name = nameof(ButtonControl), Type = typeof(Button))]
[TemplatePart(Name = nameof(ContentControl), Type = typeof(ContentControl))]
public sealed class CustomButton : ContentControl
{
    public static readonly DependencyProperty IsButtonEnabledProperty = DependencyProperty.Register(
        nameof(IsButtonEnabled),
        typeof(bool),
        typeof(CustomButton),
        new PropertyMetadata(true));

    public static readonly DependencyProperty IsContentEnabledProperty = DependencyProperty.Register(
        nameof(IsContentEnabled),
        typeof(bool),
        typeof(CustomButton),
        new PropertyMetadata(true));

    public CustomButton()
    {
        this.DefaultStyleKey = typeof(CustomButton);
    }

    public bool IsButtonEnabled
    {
        get => (bool)GetValue(IsButtonEnabledProperty);
        set => SetValue(IsButtonEnabledProperty, value);
    }

    public bool IsContentEnabled
    {
        get => (bool)GetValue(IsContentEnabledProperty);
        set => SetValue(IsContentEnabledProperty, value);
    }

    private Button? ButtonControl { get; set; }

    private ContentControl? ContentControl { get; set; }

    protected override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        ButtonControl = GetTemplateChild(nameof(ButtonControl)) as Button;

        if (GetTemplateChild(nameof(ContentControl)) is ContentControl contentControl)
        {
            if (ContentControl is not null)
            {
                ContentControl.SizeChanged -= ContentControl_SizeChanged;
            }

            ContentControl = contentControl;
            ContentControl.SizeChanged += ContentControl_SizeChanged;
        }
    }

    private void ContentControl_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        if (sender is ContentControl contentControl &&
            ButtonControl is not null)
        {
            ButtonControl.Width = contentControl.ActualWidth + 20.0;
            ButtonControl.Height = contentControl.ActualHeight + 20.0;
        }
    }
}

并像这样使用它:

主窗口.xaml

<Grid>
    <local:CustomButton
        IsButtonEnabled="False"
        IsContentEnabled="True">
        <ComboBox
            SelectedIndex="0"
            SelectionChanged="ComboBox_SelectionChanged">
            <x:String>Blue</x:String>
            <x:String>Green</x:String>
            <x:String>Red</x:String>
        </ComboBox>
    </local:CustomButton>
</Grid>

相关问题