Skip to content

ButtonGroup ItemsSource does not work #1753

@Joe-Fernico

Description

@Joe-Fernico

Describe the bug

ButtonGroup works fine if you add buttons manually, however I want to use the ItemsSource property to bind to a list on my view model to use dynamically generate the buttons. However the ButtonGroup.OnRender method casts each item to a ButtonBase, which if using ItemsSource binding, will throw an error

Image

Steps to reproduce the bug

<hc:ButtonGroup Style="{StaticResource ButtonGroupSolid}"
                Effect="{StaticResource DropShadowBrush}"
                ItemsSource="{Binding DateRanges}">
    <hc:ButtonGroup.ItemTemplate>
        <DataTemplate>
            <RadioButton Content="{Binding Name}"
                         GroupName="DateRangeGroup" />
        </DataTemplate>
    </hc:ButtonGroup.ItemTemplate>
</hc:ButtonGroup>

View Model:

public class MainViewModel
{
public ObservableCollection<DateRange> DateRanges {get;}
}

DateRange class:

public class DateRange
{
public string Name {get;}
}

Expected behavior

A dynamically created button group from the binding of ItemsSource

Screenshots

No response

NuGet package version

HandyControl 3.5.1

IDE

Visual Studio 2022

Framework type

.Net 9.0

Windows version

Windows 11 (22000)

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions