Silverlight–IValueConverter

Silverlight–IValueConverter

1.MSDN 相關資料網址

 

1.定義

    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return 0;
            }
            else
            {
                try
                {
                    return Int32.Parse(value.ToString());
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }            
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {            
            return value;
        }
    }

(簡單的字串轉數值)

 

2.設定

        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Assets/Styles.xaml"/> 
                <ResourceDictionary>
                    <app:ApplicationResources x:Key="ApplicationResources" />
                    <app:NotOperatorValueConverter x:Key="NotOperatorValueConverter" /> <!-- 加入到資源檔 --> 
                    <app:IntConvert x:Key="InvConvert" />
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>                
    </Application.Resources>

(App.Xmal)

3.使用