WPF:在datatemplate中定义的组合框中添加一个按钮 [英] WPF: Add a button in a combobox defined in datatemplate

查看:78
本文介绍了WPF:在datatemplate中定义的组合框中添加一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的xaml中定义了这个 ItemsControl

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>

我需要添加到每个创建的 ComboBox 中这样,由于 ComboBox 具有源,因此可能将 Button 放置在下拉列表的底部。当显示 UserControl 时,我当前的代码引发异常(XamlParseError)。

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?

推荐答案

您可以使用 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>

这篇关于WPF:在datatemplate中定义的组合框中添加一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆