WPF: Add a button in a combobox defined in datatemplate

时间:2017-06-15 10:18:27

标签: c# wpf combobox

I have this ItemsControl defined in my xaml

<ItemsControl Grid.ColumnSpan="3" Grid.Row="1" HorizontalAlignment="Center"  ItemsSource="{Binding MyItems, Mode=TwoWay}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="200"/>
                    <ColumnDefinition Width="200"/>
                    <ColumnDefinition Width="200"/>
                </Grid.ColumnDefinitions>
                <Label HorizontalContentAlignment="Center" Grid.Column="0" Content="{Binding FirstProperty}"/>
                <Label HorizontalContentAlignment="Center" Grid.Column="1" Content="{Binding SecondProperty}"/>
                <Label HorizontalContentAlignment="Center" Grid.Column="2" Content="{Binding ThirdProperty}"/>
                <ComboBox HorizontalAlignment="Center" ItemsSource="{Binding CBSource}" Grid.Column="2" Width="140" Visibility="{Binding HasCombobox, Converter={StaticResource BoolToVis}}">
                    <ComboBoxItem>
                        <Button Content="INeedAButtonHere"></Button>
                    </ComboBoxItem>
                </ComboBox>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

I need to add to each ComboBox created in this way a Button, possibly placed on the bottom of the dropdown since the ComboBox has a source. My current code raise an exception (XamlParseError) when the UserControl is shown. How can be this done?

1 个答案:

答案 0 :(得分:1)

您可以使用CompositeCollection

<ItemsControl Grid.ColumnSpan="3" Grid.Row="1" HorizontalAlignment="Center"
              ItemsSource="{Binding MyItems}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.Resources>
                    <CollectionViewSource x:Key="cvs" Source="{Binding CBSource}" />
                </Grid.Resources>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="200"/>
                    <ColumnDefinition Width="200"/>
                    <ColumnDefinition Width="200"/>
                </Grid.ColumnDefinitions>
                <Label HorizontalContentAlignment="Center" Grid.Column="0" Content="TEST"/>
                <Label HorizontalContentAlignment="Center" Grid.Column="1" Content="{Binding SecondProperty}"/>
                <Label HorizontalContentAlignment="Center" Grid.Column="2" Content="{Binding ThirdProperty}"/>
            <ComboBox HorizontalAlignment="Center" Grid.Column="2" Width="140" Visibility="{Binding HasCombobox, Converter={StaticResource BoolToVis}}">
                    <ComboBox.ItemsSource>
                        <CompositeCollection>
                            <CollectionContainer Collection="{Binding Source={StaticResource cvs}}" />
                            <ComboBoxItem>
                                <Button Content="INeedAButtonHere"></Button>
                            </ComboBoxItem>
                        </CompositeCollection>
                    </ComboBox.ItemsSource>
                </ComboBox>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>