wpf Visibility 前臺xml 綁定int類型值,通過再轉化實現對應屬性值更改

對應的工具類:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Data;

namespace wpfYourSystem.Util
{
    /// <summary>
    /// 一個通用的類型轉換器,可以提供更多轉換控制參數
    /// </summary>
    public class GenericTypeConverter : IValueConverter
    {

        /// <summary>
        /// 正向鍵值對字典
        /// </summary>
        private Dictionary<string, string> Alias { get; set; }

        /// <summary>
        /// 反向鍵值對字典
        /// </summary>
        private Dictionary<string, string> BackAlias { get; set; }

        private string aliasStrTemp = "";
        /// <summary>
        /// 解析轉換規則
        /// </summary>
        /// <param name="aliasStr">規則字符串</param>
        private void ParseAliasByStr(string aliasStr)
        {
            if (aliasStrTemp == aliasStr)
                return;
            aliasStrTemp = aliasStr;
            Alias = new Dictionary<string, string>();
            BackAlias = new Dictionary<string, string>();

            string content = aliasStr;

            if (aliasStr.Contains("Alias="))
            {
                content = aliasStr.Split('=')[1];
            }
            Alias = new Dictionary<string, string>();
            string[] arr1 = content.Split('|');
            foreach (string item in arr1)
            {
                string[] arr2 = item.Split(':');
                if (arr2[0] != "other")
                    BackAlias.Add(arr2[1], arr2[0]);
                Alias.Add(arr2[0], arr2[1]);
            }
        }

        private object ConvertCommon(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture, bool isBack)
        {
            if (value == null || string.IsNullOrWhiteSpace(parameter.ToString()))
                return null;
            object ret = value;//如果沒有匹配返回傳入的值

            ParseAliasByStr(parameter.ToString());
            Dictionary<string, string> alias;
            if (isBack)
                alias = BackAlias;
            else
                alias = Alias;

            //綁定的值
            string bindingValue = value.ToString();
            if (alias.ContainsKey(bindingValue))
                ret = StringToTargetType(alias[bindingValue], targetType);
            else if (alias.ContainsKey("other"))
                ret = StringToTargetType(alias["other"], targetType);
            else if (alias.ContainsKey("else"))
                ret = StringToTargetType(alias["else"], targetType);
            return ret;
        }

        /// <summary>
        /// 字符串轉換成目標類型,如需添加一個目標類型只需在該方法中添加一個類型判斷之後轉換
        /// </summary>
        /// <param name="strValue"></param>
        /// <param name="targetType"></param>
        /// <returns></returns>
        private object StringToTargetType(string strValue, Type targetType)
        {
            object ret = null;
            //目標類型 string
            if (targetType == typeof(string) || targetType == typeof(char))
            {
                ret = strValue;
            }
            //目標類型 char
            if (targetType == typeof(char))
            {
                if (strValue.Length == 1)
                    ret = strValue;
            }
            //目標類型 int
            if (targetType == typeof(int))
            {
                int temp;
                if (int.TryParse(strValue, out temp))
                    ret = temp;
                else
                    ret = 0;
            }
            //目標類型 double
            if (targetType == typeof(double))
            {
                double temp;
                if (double.TryParse(strValue, out temp))
                    ret = temp;
                else
                    ret = 0;
            }
            //目標類型 float
            if (targetType == typeof(float))
            {
                float temp;
                if (float.TryParse(strValue, out temp))
                    ret = temp;
                else
                    ret = 0;
            }
            //目標類型 decimal
            if (targetType == typeof(decimal))
            {
                decimal temp;
                if (decimal.TryParse(strValue, out temp))
                    ret = temp;
                else
                    ret = 0;
            }
            //目標類型 bool? bool
            if (targetType == typeof(bool?) || targetType == typeof(bool))
            {
                bool temp;
                if (bool.TryParse(strValue, out temp))
                    ret = temp;
                else
                    ret = false;
            }

            //目標類型 Visibility
            if (targetType == typeof(Visibility))
            {
                switch (strValue.ToLower())
                {

                    case "collapsed":
                        ret = Visibility.Collapsed;
                        break;
                    case "hidden":
                        ret = Visibility.Hidden;
                        break;
                    case "visible":
                        ret = Visibility.Visible;
                        break;
                    default:
                        ret = Visibility.Visible;
                        break;
                }
            }



            return ret;
        }


        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            return ConvertCommon(value, targetType, parameter, culture, false);
        }

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

            return ConvertCommon(value, targetType, parameter, culture, true);

        }
    }
 }

 

Resouce:

  xmlns:util="clr-namespace:wpfYourSystem.Util" 
 <util:GenericTypeConverter x:Key="anyTypeConverter"/>

XML:

  <TextBlock Text="已過號" HorizontalAlignment="Right" FontWeight="Bold" FontSize="20" 
                                           Visibility="{Binding IntState,Converter={StaticResource anyTypeConverter},ConverterParameter='2:Visible|other:Collapsed'}"
                                           Foreground="Red"  Margin="0 0 20 0" VerticalAlignment="Center" />

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章