How to extend a Control in Avalonia?

China☆狼群 提交于 2021-01-27 13:04:24

问题


I want to extend the default dropdown with some functionality. The custom dropdown should behave like a default dropdown in the .xaml file, so it should be possible to add items to it.

Unfortunately, it does not seem to work like in WPF. That's my approach:

MainWindow.xaml: (added the namespace)

<local:myCustomDropDown>
  <DropDownItem>1</DropDownItem>
  <DropDownItem>2</DropDownItem>
</local:myCustomDropDown>

myCustomDropDown.xaml:

<DropDown xmlns="https://github.com/avaloniaui"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          x:Class="AvaloniaApplication2.myCustomDropDown">
</DropDown>

Code behind:

public class myCustomDropDown : DropDown
{
    public myCustomDropDown()
    {
        this.InitializeComponent();
    }

    private void InitializeComponent()
    {
        AvaloniaXamlLoader.Load(this);
    }
}

This seems to compile without errors or warnings, but the control does not show.


回答1:


You need to also apply DropDown's control styles. You can do that by changing the style key like this: https://github.com/AvaloniaUI/Avalonia/blob/353c24b8abdeaae2a1c543665ef46c2161573e9f/src/Avalonia.Controls/UserControl.cs#L31



来源:https://stackoverflow.com/questions/51746650/how-to-extend-a-control-in-avalonia

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!