WPF - 包含按钮的项目的组合框

Ran*_*der 0 wpf wpf-controls

我在窗口中有一个ComboBox控件.我正在从XML文件中读取运行时组成ComboBox的项目.我想要做的是在每个项目中包含一个Button对象,这样如果用户单击该按钮,我可以根据与该按钮关联的项目采取适当的操作.当用户下拉列表时,它可能看起来像这样:

Item 1         [Button]
Another Item   [Button]
Item 3         [Button]
Run Code Online (Sandbox Code Playgroud)

我没有足够的经验与WPF知道这是否可行,但一位同事说它应该是可行的.有没有人这样做过,或者知道怎么做?请记住,ComboBox控件是通过XAML创建的,但这些项是在运行时创建的.

RQD*_*QDQ 6

这是一种方式:

<ComboBox x:Name="MyComboBox" VerticalAlignment="Top" HorizontalAlignment="Left">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>

                        <Label Content="{Binding}" Width="100" />
                        <Button Grid.Column="1">Do Something</Button>
                    </Grid>

                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
Run Code Online (Sandbox Code Playgroud)

在后面的代码中,我使用了一个简单的字符串列表:

List<String> strings = new List<string>();

strings.Add("Item 1");
strings.Add("Another Item");
strings.Add("Item 3");

MyComboBox.ItemsSource = strings;
Run Code Online (Sandbox Code Playgroud)

这就是它的样子:

在此输入图像描述

编辑:这是一个关于如何将网格添加到ComboBox下拉列表的资源(这超出了SO答案的范围):

http://www.eggheadcafe.com/tutorials/aspnet/e8585e81-34c8-4808-ae3e-b8b35d738842/wpf-datagrid-as-combobox.aspx