gpt4 book ai didi

c# - 如何通过 XAML 手动将静态项目绑定(bind)到 SplitButton (mahapps)

转载 作者:行者123 更新时间:2023-11-30 20:46:10 28 4
gpt4 key购买 nike

我如何手动添加例如 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 但没有背后的代码示例。

最佳答案

这是 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;
}

这是它在运行时的样子:

This is how it look at runtime

这是您要找的吗?

关于c# - 如何通过 XAML 手动将静态项目绑定(bind)到 SplitButton (mahapps),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27337092/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com