问题
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