/*
* Copyright (c) 2022 LG Electronics Inc.
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
*/
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
namespace AuroraUIComponent.UserControls
{
///
/// UCRadioGroup.xaml에 대한 상호 작용 논리
///
public partial class UCRadioGroup : UserControl
{
public enum ePanelType { Uniform, StackVertical, StackHorizontal }
public UCRadioGroup()
{
InitializeComponent();
}
public static readonly DependencyProperty ItemListProperty
= DependencyProperty.Register(
nameof(ItemList),
typeof(List),
typeof(UCRadioGroup), new FrameworkPropertyMetadata(
null,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public List ItemList {
get { return (List)GetValue(ItemListProperty); }
set { SetValue(ItemListProperty, value); }
}
public static readonly DependencyProperty GroupMarginProperty
= DependencyProperty.Register(
nameof(GroupMargin),
typeof(Thickness),
typeof(UCRadioGroup), new FrameworkPropertyMetadata(
new Thickness(30,0,30,0),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public Thickness GroupMargin {
get { return (Thickness)GetValue(GroupMarginProperty); }
set { SetValue(GroupMarginProperty, value); }
}
public static readonly DependencyProperty RadioButtonMarginProperty
= DependencyProperty.Register(
nameof(RadioButtonMargin),
typeof(Thickness),
typeof(UCRadioGroup), new FrameworkPropertyMetadata(
new Thickness(0, 0, 0, 0),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public Thickness RadioButtonMargin {
get { return (Thickness)GetValue(RadioButtonMarginProperty); }
set { SetValue(RadioButtonMarginProperty, value); }
}
public static readonly DependencyProperty SelectedItemProperty
= DependencyProperty.Register(
nameof(SelectedItem),
typeof(string),
typeof(UCRadioGroup),new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault
, OnSelectedItemChange));
public string SelectedItem {
get { return (string)GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
public static readonly DependencyProperty GroupBoxNameProperty
= DependencyProperty.Register(
nameof(GroupBoxName),
typeof(string),
typeof(UCRadioGroup), new FrameworkPropertyMetadata(
null,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public string GroupBoxName {
get { return (string)GetValue(GroupBoxNameProperty); }
set { SetValue(GroupBoxNameProperty, value); }
}
public static readonly DependencyProperty DefaultCheckedProperty
= DependencyProperty.Register(
nameof(DefaultChecked),
typeof(int),
typeof(UCRadioGroup), new FrameworkPropertyMetadata(
0,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public static readonly DependencyProperty PanelTypeProperty
= DependencyProperty.Register(
nameof(PanelType),
typeof(ePanelType),
typeof(UCRadioGroup),
new PropertyMetadata(
ePanelType.StackHorizontal, OnPanelTypeChange));
public ePanelType PanelType {
get { return (ePanelType)GetValue(PanelTypeProperty); }
set { SetValue(PanelTypeProperty, value); }
}
///
/// Group 중 가장 처음에 Checked 될 Index, 0 ~
///
public int DefaultChecked {
get { return (int)GetValue(DefaultCheckedProperty); }
set { SetValue(DefaultCheckedProperty, value); }
}
#region UI Event
private void RadioButton_Click(object sender, RoutedEventArgs e)
{
SelectedItem = (sender as RadioButton).Content.ToString();
}
private void UCRadioGroup_Loaded(object sender, RoutedEventArgs e)
{
if((sender as RadioButton).Content is string strContent && strContent == ItemList[DefaultChecked]) {
(sender as RadioButton).IsChecked = true;
SelectedItem = (sender as RadioButton).Content.ToString();
}
}
private static void OnSelectedItemChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if ((bool)d.GetValue(IsVisibleProperty) && e.OldValue != null) {
if ((d as UCRadioGroup).RadioButtonsGroup.Items.Contains(e.NewValue) && (d as UCRadioGroup).RadioButtonsGroup.ItemContainerGenerator.Status != GeneratorStatus.NotStarted) {
var container = ((d as UCRadioGroup).RadioButtonsGroup.ItemContainerGenerator.ContainerFromItem(e.NewValue)) as ContentPresenter;
var rbtn = container.ContentTemplate.FindName("rBtn", container) as RadioButton;
rbtn.IsChecked = true;
}
}
}
private static void OnPanelTypeChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if(Enum.IsDefined(typeof(ePanelType), e.NewValue)) {
(d as UCRadioGroup).RadioButtonsGroup.ItemsPanel = (d as UCRadioGroup).RadioButtonsGroup.FindResource(e.NewValue.ToString()) as ItemsPanelTemplate;
}
}
#endregion
}
}