You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.9 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace DependencyPropertySample2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
// dependency property
public static readonly DependencyProperty MyProperty = DependencyProperty.Register
("MyColor", typeof(string), typeof(MainWindow), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnMyPropertyChanged)));
public MainWindow()
{
InitializeComponent();
}
public string MyColor
{
get { return (string)GetValue(MyProperty); }
set { SetValue(MyProperty, value); }
}
private static void OnMyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MainWindow win = d as MainWindow;
if (win == null)
return;
SolidColorBrush brush = new BrushConverter().ConvertFromString(e.NewValue.ToString()) as SolidColorBrush;
if (brush == null)
return;
win.Background = brush;
win.Title = (e.OldValue == null) ? "Previous: None" : $"Previous: {e.OldValue.ToString()}";
win.tbMain.Text = e.NewValue.ToString();
}
private void ContextMenu_Click(object sender, RoutedEventArgs e)
{
string str = (e.Source as MenuItem).Header as string;
if (string.IsNullOrEmpty(str))
return;
this.MyColor = str;
}
}
}