2026-06-05 13:44:39 +09:00

198 lines
7.3 KiB
C#

/*
* Copyright (c) 2022 LG Electronics Inc.
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
*/
using AuroraUIComponent.UserControls.KeyValueBase;
using System;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace AuroraUIComponent.UserControls
{
/// <summary>
/// Interaction logic for UCKeyValueAuto.xaml
///
/// UCKeyValue, UCKeyValueString, UCKeyValueBool 등을 DataType에 따라 자동으로 표시해주기 위한 wrapper class
/// 지원 DataType : double, int, string, bool
/// </summary>
public partial class UCKeyValueAuto : UCKeyValueNumber
{
public static readonly DependencyProperty DataTypeProperty
= DependencyProperty.Register(
nameof(DataType),
typeof(Type),
typeof(UCKeyValueAuto),
new PropertyMetadata(null, OnDataTypeChanged));
public Type DataType {
get { return (Type)GetValue(DataTypeProperty); }
set { SetValue(DataTypeProperty, value); }
}
private static void OnDataTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as UCKeyValueAuto;
control.UpdateFromDataType((Type)e.NewValue);
}
#region UCKeyValueString
public static readonly DependencyProperty ValueTextProperty
= DependencyProperty.Register(
nameof(ValueText),
typeof(string),
typeof(UCKeyValueAuto),
new FrameworkPropertyMetadata(
"",
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault
));
public string ValueText {
get { return (string)GetValue(ValueTextProperty); }
set { SetValue(ValueTextProperty, value); }
}
#endregion
#region UCKeyValueCheckBox
public static readonly DependencyProperty ValueBoolProperty
= DependencyProperty.Register(
nameof(ValueBool),
typeof(bool),
typeof(UCKeyValueAuto),
new FrameworkPropertyMetadata(
false,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault
));
public bool ValueBool {
get { return (bool)GetValue(ValueBoolProperty); }
set { SetValue(ValueBoolProperty, value); }
}
#endregion
protected UCKeyValue UCKeyValueInt;
protected UCKeyValue UCKeyValueDouble;
protected UCKeyValueString UCKeyValueString;
protected UCKeyValueCheckBox UCKeyValueBool;
public void UpdateFromDataType(Type newValue)
{
if (newValue == typeof(double)) {
cpValue.Content = UCKeyValueDouble;
} else if (newValue == typeof(int)) {
cpValue.Content = UCKeyValueInt;
} else if (newValue == typeof(string)) {
cpValue.Content = UCKeyValueString;
} else if (newValue == typeof(bool)) {
cpValue.Content = UCKeyValueBool;
}
}
private void RegisterBindings(DependencyProperty prop)
{
Binding binding = new Binding(prop.ToString());
binding.Source = this;
if (prop != DecimalPlacesProperty) {
UCKeyValueInt.SetBinding(prop, binding);
}
UCKeyValueDouble.SetBinding(prop, binding);
UCKeyValueString.SetBinding(prop, binding);
UCKeyValueBool.SetBinding(prop, binding);
}
public UCKeyValueAuto() : base()
{
InitializeComponent();
UCKeyValueInt = new UCKeyValue();
UCKeyValueDouble = new UCKeyValue();
UCKeyValueString = new UCKeyValueString();
UCKeyValueBool = new UCKeyValueCheckBox();
#region Binding Properties for each UC
RegisterBindings(KeyTextProperty);
RegisterBindings(KeyHorizontalAlignmentProperty);
RegisterBindings(ValueHorizontalAlignmentProperty);
RegisterBindings(KeyTextSizeProperty);
RegisterBindings(ValueTextSizeProperty);
RegisterBindings(KeyTextWidthProperty);
RegisterBindings(ValueTextWidthProperty);
RegisterBindings(KeyTextFgColorProperty);
RegisterBindings(ValueTextFgColorProperty);
RegisterBindings(KeyTextBgColorProperty);
RegisterBindings(ValueTextBgColorProperty);
RegisterBindings(IsReadOnlyProperty);
RegisterBindings(DecimalPlacesProperty);
RegisterBindings(UnitProperty);
RegisterBindings(ShowKeypadProperty);
// Value / ValueTextProperty
Binding bindingValue = new Binding(ValueProperty.ToString());
bindingValue.Source = this;
UCKeyValueInt.SetBinding(ValueProperty, bindingValue);
UCKeyValueDouble.SetBinding(ValueProperty, bindingValue);
Binding bindingValueText = new Binding(ValueTextProperty.ToString());
bindingValueText.Source = this;
UCKeyValueString.SetBinding(UCKeyValueString.ValueTextProperty, bindingValueText);
Binding bindingValueBool = new Binding(ValueBoolProperty.ToString());
bindingValueBool.Source = this;
UCKeyValueBool.SetBinding(UCKeyValueCheckBox.ValueBoolProperty, bindingValueBool);
#endregion
UCKeyValueDouble.TextValue.LostFocus += TextBoxOnLostFocus;
cpValue.Content = UCKeyValueInt;
}
private void TextBoxOnLostFocus(object sender, RoutedEventArgs e)
{
Value = ParseString2Decimal(UCKeyValueDouble.TextValue.Text);
}
protected override void ChangeThousandSeparator(decimal value)
{
//change formatting based on this flag
if (ThousandSeparator) {
if (UCKeyValueDouble.TextValue.Text != null) {
UCKeyValueDouble.TextValue.Text = value.ToString("N", Culture);
}
} else {
if (UCKeyValueDouble.TextValue.Text != null) {
UCKeyValueDouble.TextValue.Text = value.ToString($"F", Culture);
}
}
}
private void SetTextHidden(Grid grid)
{
// KeyText 가 없으므로, 해당 grid 의 width 를 0 으로 만든다
if (grid.ColumnDefinitions.Count > 0) {
grid.ColumnDefinitions[0].Width = new GridLength(0);
}
if (grid.RowDefinitions.Count > 0) {
grid.RowDefinitions[0].Height = new GridLength(0);
}
}
#region For UCKeyValueString & UCKeyValueCheckBox
protected override void KeyTextChanged(string newValue)
{
if (string.IsNullOrEmpty(newValue)) {
//SetTextHidden(UCKeyValueInt.GridParent);
//SetTextHidden(UCKeyValueDouble.GridParent);
SetTextHidden(UCKeyValueString.GridParent);
SetTextHidden(UCKeyValueBool.GridParent);
}
}
#endregion
}
}