WPF How to use a Button as ComboBoxItem

I’d like to include a few Button inside a ComboBox directly in XAML (not populated by a view model):

<ComboBox>
    <Button Command="{Binding PerformAction1}" Content="Action1" />
    <Button Command="{Binding PerformAction2}" Content="Action2" />
</ComboBox>

So, the ComboBoxItem correspond to a Button in order to directly execute a Command when clicked.

1st issue: When I click a button, the ComboBoxItem is not selected. I can change the ItemTemplate in order to add a Button_Click callback.

<ComboBox.ItemTemplate>
    <DataTemplate>
        <Button Click="Button_Click" Content="{Binding}" />
    </DataTemplate>
</ComboBox.ItemTemplate>
private void Button_Click(object sender, RoutedEventArgs e)
{
    var button = sender as Button;

    var content = button.Content as Button; // Gets the the actual button within the button from the template.

    //content.Command.Execute(null); // content.Command is null.

    var cbi = UIHelpers.FindParent<ComboBoxItem>(button);

    cbi.IsSelected = true;
}

2nd issue: The Command binding is not executed from XAML and it seems that I can’t get it from the code-behind.

Does someone have an idea how to solve it?

8

I’ll write right away that I agree with Clemens and ASh – the solution you’ve chosen is terrible. (See my reply to ASh’ comment)

But for educational purposes I’ll give an explanation and show a solution.

The button intercepts MouseLeftButtonDown/Up events, marks them as handled Handled= true, and raises the Click event. Since MouseLeftButtonDown/Up events have already been handled, the parent elements (including ComboBoxItem) “don’t see” them.
Your use of the Click event is one of the solutions to this problem.

Using a nested button is definitely excessive.

It would also be more optimal to transfer most of the logic to XAML, but only do the most necessary in code.

An example of implementation:

    <ComboBox>
        <Button Command="{Binding PerformAction1}" Content="Action1"
                Click="OnSelectedItem"
                Tag="{Binding SelectedItem, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Selector}}}"/>
        <Button Command="{Binding PerformAction2}" Content="Action2" 
                Click="OnSelectedItem"
                Tag="{Binding SelectedItem, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Selector}}}"/>
    </ComboBox>
    <x:Code>
        <![CDATA[
            private void OnSelectedItem(object sender, RoutedEventArgs e)
            {
                Button btn = (Button)sender;
                btn.Tag = btn;
            }
        ]]>
    </x:Code>

You can also use interception of the bubble-event at the ComboBox level. This will simplify the XAML a little.

    <ComboBox ButtonBase.Click="OnBubbleСlick">
        <Button Command="{Binding PerformAction1}" Content="Action1"/>
        <Button Command="{Binding PerformAction2}" Content="Action2"/>
    </ComboBox>
    <x:Code>
        <![CDATA[
            private void OnBubbleСlick(object sender, RoutedEventArgs e)
            {
                ((Selector)sender).SelectedItem = e.OriginalSource;
            }
        ]]>
    </x:Code>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật