• WPF中TypeConverter类的使用


    前台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>
    后台代码:

    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;
            }
        }
    }
     

    深度解析 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
     

  • 相关阅读:
    tomcat与resin的比较
    Linux Resin 安装配置
    [BZOJ3456]城市规划
    ZJOI 2017 仙人掌
    「LibreOJ NOI Round #1」动态几何问题
    [SDOI2015]约数个数和
    codeforce 940F
    codeforce 940F
    codeforce 940E
    [NOI2009]植物大战僵尸
  • 原文地址:https://www.cnblogs.com/blosaa/p/2249274.html
Copyright © 2020-2023  润新知