前台xaml代码:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:HuMain x:Key="humain" Name="Time" Child="LitterTime">
</local:HuMain>
</Window.Resources>
<Grid>
<Button Content="单击我吧" Height="20" Width="100" Name="button1" Click="button1_click" ></Button>
</Grid>
</Window>
后台代码:xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:HuMain x:Key="humain" Name="Time" Child="LitterTime">
</local:HuMain>
</Window.Resources>
<Grid>
<Button Content="单击我吧" Height="20" Width="100" Name="button1" Click="button1_click" ></Button>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Globalization;
namespace WpfApplication1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_click(object sender, RoutedEventArgs e)
{
HuMain t_HuMain = FindResource("humain") as HuMain;
MessageBox.Show(t_HuMain.Child.Name);
}
}
[TypeConverterAttribute(typeof(NameToHuMainTypeConvert))]
public class HuMain
{
public string Name { get; set; }
public HuMain Child { get; set; }
}
public class NameToHuMainTypeConvert : TypeConverter
{
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string name = value.ToString();
HuMain humain = new HuMain();
humain.Name = name;
return humain;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Globalization;
namespace WpfApplication1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_click(object sender, RoutedEventArgs e)
{
HuMain t_HuMain = FindResource("humain") as HuMain;
MessageBox.Show(t_HuMain.Child.Name);
}
}
[TypeConverterAttribute(typeof(NameToHuMainTypeConvert))]
public class HuMain
{
public string Name { get; set; }
public HuMain Child { get; set; }
}
public class NameToHuMainTypeConvert : TypeConverter
{
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string name = value.ToString();
HuMain humain = new HuMain();
humain.Name = name;
return humain;
}
}
}
深度解析 TypeConverter & TypeConverterAttribute (一)
http://www.cnblogs.com/ericwen/archive/2007/12/11/typeconverter.html
http://www.cnblogs.com/ericwen/archive/2007/12/12/TypeConvertAttribute.html#1774123