Avalonia样式选择器Doen' t在派生类上工作

发布于 2025-01-28 17:23:29 字数 2045 浏览 4 评论 0原文

我有一个问题,可以将自定义控件从按钮派生出来。

我继承了按钮类,以添加依赖关系:

public class IconButton : Button, IStyleable
{
    Type IStyleable.StyleKey => typeof(Button);

    public static readonly StyledProperty<string> IconProperty =
        AvaloniaProperty.Register<IconButton, string>(nameof(Icon));

    public string Icon
    {
        get { return GetValue(IconProperty); }
        set { SetValue(IconProperty, value); }
    }
}

现在我想创建一个仅针对此派生类的样式:

    <Styles xmlns="https://github.com/avaloniaui"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:i="clr-namespace:Projektanker.Icons.Avalonia;assembly=Projektanker.Icons.Avalonia"
            xmlns:cc="*****.*****.CustomControls">
        <Design.PreviewWith>
            <Border Padding="20">
              <StackPanel Orientation="Vertical">
                <cc:IconButton Content="hello custom" Icon="fab fa-github"/>
                <Button Content="hello"/>
              </StackPanel>
            </Border>
        </Design.PreviewWith>
    
      <Style Selector=":is(cc|IconButton)">
        <Setter Property="Background" Value="Red"/>
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate>
              <StackPanel Orientation="Horizontal">
                <i:Icon Value="{TemplateBinding Icon}" />
                <TextBlock Text="{TemplateBinding Content}" />
              </StackPanel>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
    </Styles>

“在此处输入映像”

如您所见,内容不是我的控制模板,因为图标不是我的控制模板可见,背景不是红色。因此,我无法使用我的依赖性。

有什么建议还是该语言不支持? 我阅读了用于选择器的文档(****)旨在支持派生类型,因此我希望我的代码能够正常工作:(

thx寻求帮助。

I'm having an issue to style my custom control derived from button.

I inherited the Button class to add a DependencyProperty to it:

public class IconButton : Button, IStyleable
{
    Type IStyleable.StyleKey => typeof(Button);

    public static readonly StyledProperty<string> IconProperty =
        AvaloniaProperty.Register<IconButton, string>(nameof(Icon));

    public string Icon
    {
        get { return GetValue(IconProperty); }
        set { SetValue(IconProperty, value); }
    }
}

Now I want to create a style targetting only this derived class:

    <Styles xmlns="https://github.com/avaloniaui"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:i="clr-namespace:Projektanker.Icons.Avalonia;assembly=Projektanker.Icons.Avalonia"
            xmlns:cc="*****.*****.CustomControls">
        <Design.PreviewWith>
            <Border Padding="20">
              <StackPanel Orientation="Vertical">
                <cc:IconButton Content="hello custom" Icon="fab fa-github"/>
                <Button Content="hello"/>
              </StackPanel>
            </Border>
        </Design.PreviewWith>
    
      <Style Selector=":is(cc|IconButton)">
        <Setter Property="Background" Value="Red"/>
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate>
              <StackPanel Orientation="Horizontal">
                <i:Icon Value="{TemplateBinding Icon}" />
                <TextBlock Text="{TemplateBinding Content}" />
              </StackPanel>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
    </Styles>

enter image description here

AS you can see, the content is not my control template as the icon is not visible and the background is not red. Therefore I can't use my dependencyproperty.

Any suggestions or is it not supported by the language?
I read on the documentation that is(****) for selector is intended to support derived type so I would expect my code to be working :(

Thx for help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏の忆 2025-02-04 17:23:30

尝试删除可删除的和您的Overriden StyleKey。您告诉选择器,它应该寻找按钮样式而不是您的样式。

愉快的编码
蒂姆

PS我现在无法自行测试,因此,如果它不起作用,请在这里告诉我。


更新:我只是做了一个空白的新项目并测试了您的代码。删除stylekey后,它起作用:

“在此处输入映像”

这是修改的iconbutton

public class IconButton : Button
{
    public static readonly StyledProperty<string> IconProperty =
        AvaloniaProperty.Register<IconButton, string>(nameof(Icon));
    
    public string Icon
    {
        get { return GetValue(IconProperty); }
        set { SetValue(IconProperty, value); }
    }
}

如果您能够在Github上上传最小示例,i可以看看可能出了什么问题。


更新2:调查您的演示应用程序,我发现您使用了第三方图标lib。 LIB要求您在启动时配置特殊服务,请参阅: https://github.com/projektanker/icons.avalonia#1-register-register-icon-providers-on-app-start-up

我向您发送解决您问题的PR。

快乐的编码
蒂姆

Try to remove IStyleable and your overriden StyleKey. You are telling the selector that it should look for a Button style and not your style.

Happy coding
Tim

PS I cannot test it on my own right now, so if it doesn't work please tell me here.


Update: I just made a blank new project and tested your code. Once I remove the StyleKey, it works:

enter image description here

This is the modified IconButton:

public class IconButton : Button
{
    public static readonly StyledProperty<string> IconProperty =
        AvaloniaProperty.Register<IconButton, string>(nameof(Icon));
    
    public string Icon
    {
        get { return GetValue(IconProperty); }
        set { SetValue(IconProperty, value); }
    }
}

If you are able to upload a minimal sample on Github, I can have a look what may be wrong.


Update 2: Looking into your demo App I see that you use a third party icon lib. The lib requires you to configure a special service at start up, see: https://github.com/Projektanker/Icons.Avalonia#1-register-icon-providers-on-app-start-up

I send you a PR which fixes your issue.

enter image description here

Happy coding
Tim

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文