using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace Dominion.NET_WPF.Controls
{
///
/// Interaction logic for ucButton.xaml
///
public partial class UcButton : UserControl
{
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(UcButton),
new FrameworkPropertyMetadata(string.Empty,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure,
OnTextChanged));
private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is UcButton ctrl) ctrl.Text = (string)e.NewValue;
}
public static readonly DependencyProperty IsDefaultProperty =
DependencyProperty.Register("IsDefault", typeof(bool), typeof(UcButton),
new FrameworkPropertyMetadata(false,
FrameworkPropertyMetadataOptions.None,
OnIsDefaultChanged));
private static void OnIsDefaultChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is UcButton ctrl) ctrl.IsDefault = (bool)e.NewValue;
}
public static readonly DependencyProperty IsCancelProperty =
DependencyProperty.Register("IsCancel", typeof(bool), typeof(UcButton),
new FrameworkPropertyMetadata(false,
FrameworkPropertyMetadataOptions.None,
OnIsCancelChanged));
private static void OnIsCancelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is UcButton ctrl) ctrl.IsCancel = (bool)e.NewValue;
}
public static readonly RoutedEvent ClickEvent = EventManager.RegisterRoutedEvent(
"Click",
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(UcButton));
public event RoutedEventHandler Click
{
add { AddHandler(ClickEvent, value); }
remove { RemoveHandler(ClickEvent, value); }
}
public UcButton()
{
InitializeComponent();
}
public Thickness TextPadding
{
get { return bButton.Padding; }
set { bButton.Padding = value; }
}
public bool IsDefault
{
get { return bButton.IsDefault; }
set { bButton.IsDefault = value; }
}
public bool IsCancel
{
get { return bButton.IsCancel; }
set { bButton.IsCancel = value; }
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set
{
SetValue(TextProperty, value);
if (value.Contains('_'))
{
tbText.Inlines.Clear();
var i_ = value.IndexOf('_');
string newValue = $"{value.Substring(0, i_)}{value.Substring(i_ + 1, 1)}{value.Substring(i_ + 2)}";
var tbTemp = (TextBlock)Utilities.RenderText(newValue, NET_WPF.RenderSize.Small, false)[0];
while (tbTemp.Inlines.Any())
tbText.Inlines.Add(tbTemp.Inlines.ElementAt(0));
atHotkey.Text = value.Substring(i_, 2);
}
else
{
tbText.Text = value;
atHotkey.Text = string.Empty;
}
}
}
private void B_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
if (button != null) button.IsEnabled = false;
RaiseEvent(new RoutedEventArgs(ClickEvent));
if (button != null) button.IsEnabled = true;
}
private void UserControl_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
bButton.IsEnabled = (bool)e.NewValue;
if ((bool)e.NewValue)
{
//bButton.Background = (VisualBrush)FindResource("ActiveBrush");
atHotkey.Foreground = Brushes.Crimson;
}
else
{
//bButton.Background = (VisualBrush)FindResource("DisabledBackgroundBrush");
atHotkey.Foreground = Brushes.Gray;
}
}
}
}