Skip to content

Latest commit

 

History

History
83 lines (60 loc) · 2.56 KB

radiobuttons_itemssource.md

File metadata and controls

83 lines (60 loc) · 2.56 KB
-api-id -api-type
P:Microsoft.UI.Xaml.Controls.RadioButtons.ItemsSource
winrt property

Microsoft.UI.Xaml.Controls.RadioButtons.ItemsSource

-description

Gets or sets an object source used to generate the content of the control.

This documentation applies to WinUI 2 for UWP (for WinUI in the Windows App SDK, see the Windows App SDK namespaces).

-property-value

The object that is used to generate the content of the control. The default is null.

-remarks

For more info, design guidance, and code examples, see Radio buttons.

For more info about the behavior of the ItemsSource property, see ItemsControl.ItemsSource.

-see-also

RadioButtons, Items, ItemsControl.ItemsSource

-examples

This example shows how you can bind the control to a custom data source.

<!-- xmlns:muxc="using:Microsoft.UI.Xaml.Controls -->
 <muxc:RadioButtons Header="Background color"
                    SelectionChanged="BackgroundColor_SelectionChanged"
                    ItemsSource="{x:Bind colorOptionItems}"/>

...

<Border x:Name="ExampleBorder" Width="100" Height="100"/>
public sealed partial class MainPage : Page
{
    // Custom data item.
    public class ColorOptionDataModel
    {
        public string Label { get; set; }
        public SolidColorBrush ColorBrush { get; set; }

        public override string ToString()
        {
            return Label;
        }
    }

    List<ColorOptionDataModel> colorOptionItems;

    public MainPage1()
    {
        this.InitializeComponent();

        colorOptionItems = new List<ColorOptionDataModel>();
        colorOptionItems.Add(new ColorOptionDataModel()
            { Label = "Red", ColorBrush = new SolidColorBrush(Colors.Red) });
        colorOptionItems.Add(new ColorOptionDataModel()
            { Label = "Green", ColorBrush = new SolidColorBrush(Colors.Green) });
        colorOptionItems.Add(new ColorOptionDataModel()
            { Label = "Blue", ColorBrush = new SolidColorBrush(Colors.Blue) });
    }

    private void BackgroundColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var option = e.AddedItems[0] as ColorOptionDataModel;
        ExampleBorder.Background = option?.ColorBrush;
    }
}