如何手动添加例如2个项目(按钮),第一个项目是通过xaml预先选择的?像:
<controls:SplitButton SelectedIndex="0">
<controls:SplitButton.Items>
<Button Content="Button 1"/>
<Button Content="Button 2"/>
</controls:SplitButton.Items>
</controls:SplitButton>
Documentation告诉我使用ItemsSource Binding,但没有背后的代码示例。
答案 0 :(得分:3)
这是XAML:
<controls:SplitButton x:Name="splitButton"
SelectedIndex="1"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Grid.Column="1">
<controls:SplitButton.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock HorizontalAlignment="Left"
TextWrapping="Wrap"
Text="{Binding Text}"
VerticalAlignment="Top"/>
</Grid>
</DataTemplate>
</controls:SplitButton.ItemTemplate>
</controls:SplitButton>
这是背后的代码:
class SplitButtonItem
{
public string Text { get; set; }
}
public MainWindow()
{
InitializeComponent();
var button1 = new SplitButtonItem() { Text = "Item 01", };
var button2 = new SplitButtonItem() { Text = "Item 02", };
var buttonList = new List<SplitButtonItem>()
{
button1,
button2,
};
splitButton.ItemsSource = buttonList;
}
这是它在运行时的看法:
这是你正在寻找的吗?
答案 1 :(得分:1)
您可以使用ItemsSource
和一个可枚举的对象,并且不指定DisplayMemberPath
。
<controls:SplitButton HorizontalContentAlignment="Left"
HorizontalAlignment="Center"
VerticalContentAlignment="Center"
Width="120"
VerticalAlignment="Top">
<controls:SplitButton.ItemsSource>
<x:Array Type="system:String">
<system:String>Label 1</system:String>
<system:String>Another one</system:String>
<system:String>Works now</system:String>
</x:Array>
</controls:SplitButton.ItemsSource>
</controls:SplitButton>