HandyOrg / HandyOrg/HandyControl

ButtonGroup ItemsSource does not work

Open
#1,753 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
7.2k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at ButtonGroup.OnRender, which the report identifies as casting each ItemsSource item to ButtonBase. Reproduce the issue with the provided XAML, ItemTemplate, and DateRanges binding, then verify that dynamically generated RadioButtons render without the cast error.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.