我想用一些功能来扩展默认的下拉列表。自定义下拉列表的行为应该类似于.xaml
文件中的默认下拉列表,因此应该可以向其中添加项目。
不幸的是,它似乎不像WPF那样工作。这就是我的方法:
MainWindow.xaml: (添加了命名空间)
<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>
背后的代码:
public class myCustomDropDown : DropDown
{
public myCustomDropDown()
{
this.InitializeComponent();
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}
这似乎在编译时没有出现错误或警告,但是控件没有显示。
发布于 2018-08-09 15:40:51
您还需要应用DropDown的控件样式。您可以通过如下方式更改Style键:https://github.com/AvaloniaUI/Avalonia/blob/353c24b8abdeaae2a1c543665ef46c2161573e9f/src/Avalonia.Controls/UserControl.cs#L31:
public class UserControl : ContentControl, IStyleable
{
Type IStyleable.StyleKey => typeof(ContentControl);
https://stackoverflow.com/questions/51746650
复制相似问题