在這裏有 「ucData」(F#還有的全功能C#版本問題與C#和F#之間的「事件」不同定義):
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;
using System.ComponentModel;
namespace UCLibrary
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class ucInput : UserControl
{
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
public static DependencyProperty DescriptionProperty;
public static DependencyProperty DataProperty;
public static DependencyProperty UnitOfMeasureProperty;
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
public static readonly RoutedEvent DescriptionChangedEvent;
public static readonly RoutedEvent DataChangedEvent;
public static readonly RoutedEvent UnitOfMeasureChangedEvent;
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
private static void OnDescriptionChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
ucInput control = (ucInput)sender;
string oldValue = (string)e.OldValue;
string newValue = (string)e.NewValue;
string result = string.Format(
">>> OnPropertyChanged 'ucInput':'Description': Control Name: {0}; Value: {1} --> {2} <<<",
control.Name,
oldValue,
newValue);
System.Console.WriteLine(result);
RoutedPropertyChangedEventArgs<string> argsEvent =
new RoutedPropertyChangedEventArgs<string>(oldValue, newValue);
argsEvent.RoutedEvent = ucInput.DescriptionChangedEvent;
control.RaiseEvent(argsEvent);
}
private static void OnDataChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
ucInput control = (ucInput)sender;
double oldValue = (double)e.OldValue;
double newValue = (double)e.NewValue;
string result = string.Format(
">>> OnPropertyChanged 'ucInput':'Data': Control Name: {0}; Value: {1} --> {2} <<<",
control.Name,
oldValue,
newValue);
System.Console.WriteLine(result);
RoutedPropertyChangedEventArgs<double> argsEvent =
new RoutedPropertyChangedEventArgs<double>(oldValue, newValue);
argsEvent.RoutedEvent = ucInput.DataChangedEvent;
control.RaiseEvent(argsEvent);
}
private static void OnUnitOfMeasureChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
ucInput control = (ucInput)sender;
string oldValue = (string)e.OldValue;
string newValue = (string)e.NewValue;
string result = string.Format(
">>> OnPropertyChanged 'ucInput':'UnitOfMeasure': Control Name: {0}; Value: {1} --> {2} <<<",
control.Name,
oldValue,
newValue);
System.Console.WriteLine(result);
RoutedPropertyChangedEventArgs<string> argsEvent =
new RoutedPropertyChangedEventArgs<string>(oldValue, newValue);
argsEvent.RoutedEvent = ucInput.UnitOfMeasureChangedEvent;
control.RaiseEvent(argsEvent);
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
private static object OnCoerceDescriptionProperty(DependencyObject sender, Object data)
{
ucInput control = (ucInput)sender;
string value = (string)data;
value = value + ":";
string result = string.Format(
">>> OnCoerceValue 'ucInput':'UnitOfMeasure': Control Name: {0}; Value: {1} <<<",
control.Name,
value);
System.Console.WriteLine(result);
return (object)value;
}
private static object OnCoerceDataProperty(DependencyObject sender, Object data)
{
ucInput control = (ucInput)sender;
double value = (double)data;
string result = string.Format(
">>> OnCoerceValue 'ucInput':'UnitOfMeasure': Control Name: {0}; Value: {1} <<<",
control.Name,
value);
System.Console.WriteLine(result);
return (object)value;
}
private static object OnCoerceUnitOfMeasureProperty(DependencyObject sender, Object data)
{
ucInput control = (ucInput)sender;
string value = (string)data;
value = "[" + value + "]";
string result = string.Format(
">>> OnCoerceValue 'ucInput':'UnitOfMeasure': Control Name: {0}; Value: {1} <<<",
control.Name,
value);
System.Console.WriteLine(result);
return (object)value;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
private static bool OnValidateDescriptionProperty(Object data)
{
string result = string.Format(
">>> OnValidateValue 'ucInput':'UnitOfMeasure': TypeOf: {0} <<<",
data.GetType().ToString());
System.Console.WriteLine(result);
return (data.GetType() == typeof(String));
}
private static bool OnValidateDataProperty(Object data)
{
string result = string.Format(
">>> OnValidateValue 'ucInput':'UnitOfMeasure': TypeOf: {0} <<<",
data.GetType().ToString());
System.Console.WriteLine(result);
return (data.GetType() == typeof(Double));
}
private static bool OnValidateUnitOfMeasureProperty(Object data)
{
string result = string.Format(
">>> OnValidateValue 'ucInput':'UnitOfMeasure': TypeOf: {0} <<<",
data.GetType().ToString());
System.Console.WriteLine(result);
return (data.GetType() == typeof(String));
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
static ucInput()
{
// ------------------------------------------------------------------------------------------ //
DescriptionProperty =
DependencyProperty.Register(
"Description", typeof(string), typeof(ucInput),
new FrameworkPropertyMetadata("Description",
FrameworkPropertyMetadataOptions.Inherits,
new PropertyChangedCallback(OnDescriptionChanged),
new CoerceValueCallback(OnCoerceDescriptionProperty)),
new ValidateValueCallback(OnValidateDescriptionProperty)
);
DataProperty =
DependencyProperty.Register(
"Data", typeof(double), typeof(ucInput),
new FrameworkPropertyMetadata(0.0,
FrameworkPropertyMetadataOptions.Inherits,
new PropertyChangedCallback(OnDataChanged),
new CoerceValueCallback(OnCoerceDataProperty)),
new ValidateValueCallback(OnValidateDataProperty)
);
UnitOfMeasureProperty =
DependencyProperty.Register(
"UnitOfMeasure", typeof(string), typeof(ucInput),
new FrameworkPropertyMetadata("UM",
FrameworkPropertyMetadataOptions.Inherits,
new PropertyChangedCallback(OnUnitOfMeasureChanged),
new CoerceValueCallback(OnCoerceUnitOfMeasureProperty)),
new ValidateValueCallback(OnValidateUnitOfMeasureProperty)
);
// ------------------------------------------------------------------------------------------ //
DescriptionChangedEvent =
EventManager.RegisterRoutedEvent(
"DescriptionChanged", RoutingStrategy.Bubble,
typeof(RoutedPropertyChangedEventHandler<string>), typeof(ucInput));
DataChangedEvent =
EventManager.RegisterRoutedEvent(
"DataChanged", RoutingStrategy.Bubble,
typeof(RoutedPropertyChangedEventHandler<double>), typeof(ucInput));
UnitOfMeasureChangedEvent =
EventManager.RegisterRoutedEvent(
"UnitOfMeasureChanged", RoutingStrategy.Bubble,
typeof(RoutedPropertyChangedEventHandler<string>), typeof(ucInput));
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
public ucInput()
{
InitializeComponent();
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
[Description("Description of the Numerical Data"), Category("UserData")]
public string Description
{
get { return (string)GetValue(DescriptionProperty); }
set { SetValue(DescriptionProperty, value); }
}
[Description("Specify the Numerical Data"), Category("UserData")]
public double Data
{
get { return (double)GetValue(DataProperty); }
set { SetValue(DataProperty, value); }
}
[Description("Specify the 'Unit of Measure'"), Category("UserData")]
public string UnitOfMeasure
{
get { return (string)GetValue(UnitOfMeasureProperty); }
set { SetValue(UnitOfMeasureProperty, value); }
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
[Description("Property 'Description' Changed"), Category("UserData")]
public event RoutedPropertyChangedEventHandler<string> DescriptionChanged
{
add { AddHandler (DescriptionChangedEvent, value); }
remove { RemoveHandler(DescriptionChangedEvent, value); }
}
[Description("Property 'Data' Changed"), Category("UserData")]
public event RoutedPropertyChangedEventHandler<double> DataChanged
{
add { AddHandler(DataChangedEvent, value); }
remove { RemoveHandler(DataChangedEvent, value); }
}
[Description("Property 'UnitOfMeasure' Changed"), Category("UserData")]
public event RoutedPropertyChangedEventHandler<double> UnitOfMeasureChanged
{
add { AddHandler(UnitOfMeasureChangedEvent, value); }
remove { RemoveHandler(UnitOfMeasureChangedEvent, value); }
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
}
}
這裏下面是XAML
<UserControl x:Class="UCLibrary.ucInput"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:UCLibrary"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="507">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100*" MinWidth="100"/>
<ColumnDefinition Width="Auto" MinWidth="80"/>
<ColumnDefinition Width="70*" MinWidth="70"/>
</Grid.ColumnDefinitions>
<Label Name="lblDescription"
Margin="3"
Grid.Column="0"
Content="{Binding Description, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ucInput}}}"/>
<TextBox Name="txtData"
Margin="3"
Grid.Column="1"
Text="{Binding Data, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ucInput}}, StringFormat=N}">
</TextBox>
<Label Name="lblUnitOfMeasure"
Margin="3"
Grid.Column="2"
Content="{Binding UnitOfMeasure, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ucInput}}}"/>
</Grid>
我還在努力... – 2015-03-04 18:48:10