潜移默化学会WPF--值转换器 - AYUI框架 - 博客园

原文: 潜移默化学会WPF--值转换器 - AYUI框架 - 博客园

1. binding 后面的stringFormat的写法----连接字符串

     <TextBlock Text="{Binding Path=Qty, StringFormat=Quantity: \{0\}}" />

2.

复制代码
    [ValueConversion(typeof(decimal), typeof(string))]
    public class PriceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            decimal price = (decimal)value;
            return price.ToString("c", culture);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string price = value.ToString();
            
            decimal result;
            if (Decimal.TryParse(price, System.Globalization.NumberStyles.Any, culture, out result))
            {
                return result;
            }
            return value;
        }
    }
复制代码

用法  你懂的

复制代码
<TextBox Margin="5" Grid.Row="2" Grid.Column="1">
          <TextBox.Text>
            <Binding Path="UnitCost">
              <Binding.Converter>
                <local:PriceConverter></local:PriceConverter>
              </Binding.Converter>              
            </Binding>
          </TextBox.Text>          
        </TextBox>
复制代码

3.条件式的值转换器

复制代码
 public class PriceToBackgroundConverter : IValueConverter
    {
        public decimal MinimumPriceToHighlight
        {
            get;
            set;
        }

        public Brush HighlightBrush
        {
            get;
            set;
        }

        public Brush DefaultBrush
        {
            get;
            set;
        }

        public object Convert(object value, Type targetType, object parameter,
          System.Globalization.CultureInfo culture)
        {
            decimal price = (decimal)value;
            if (price >= MinimumPriceToHighlight)
                return HighlightBrush;
            else
                return DefaultBrush;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
          System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
复制代码

用法

先引入命名空间

然后在需要转换器的窗体中定义资源

<Window.Resources>
</local:ImagePathConverter>
    <local:PriceToBackgroundConverter x:Key="PriceToBackgroundConverter"
      DefaultBrush="{x:Null}" HighlightBrush="Orange" MinimumPriceToHighlight="10">      
    </local:PriceToBackgroundConverter>
  </Window.Resources>

用资源

     <Border DataContext="{Binding ElementName=lstProducts, Path=SelectedItem}"
              Background="{Binding Path=UnitCost, Converter={StaticResource PriceToBackgroundConverter}}"              
           Padding="7" >

例如 这是一个图片地址转成图片资源的一个转换器

复制代码
 public class ImagePathConverter : IValueConverter
    {
        private string imageDirectory = Directory.GetCurrentDirectory();
        public string ImageDirectory
        {
            get { return imageDirectory; }
            set { imageDirectory = value; }
        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string imagePath = Path.Combine(ImageDirectory, (string)value);
            return new BitmapImage(new Uri(imagePath)); 
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException("The method or operation is not implemented.");
        }
    }
复制代码

引入命名空间 ,定义资源

    <local:ImagePathConverter x:Key="ImagePathConverter"></local:ImagePathConverter>

用资源

   <Image Source="{Binding Path=ProductImagePath, Converter={StaticResource ImagePathConverter}}"
                     Width="100"
                     ></Image>

4. 多值转换器

复制代码
  public class ValueInStockConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // Return the total value of all the items in stock.
            decimal unitCost = (decimal)values[0];
            int unitsInStock = (int)values[1];
            return unitCost * unitsInStock;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
复制代码

5.含参数的转换器,前台页面上的一个例子的写法

复制代码
  <TextBlock Grid.Column="1" HorizontalAlignment="Left" Width="60" ToolTip="{Binding Number}">
                        <TextBlock.Text>
                            <Binding Path="Number" Converter="{StaticResource lengthCut}">
                                <Binding.ConverterParameter>
                                    <sys:String>m</sys:String>
                                </Binding.ConverterParameter>
                            </Binding>
                        </TextBlock.Text>
                    </TextBlock>
复制代码

本例sys是先引入    命名空间的

xmlns:sys="clr-namespace:System;assembly=mscorlib" 

6.其他stringFormat

 例如

  <TextBlock Text="{Binding Date,StringFormat={}{0:MM/dd/yyyy}}" />

  还有很多简单的 字母直接表示的

     <TextBlock Text="{Binding UnitCost,StringFormat=The Value is {0:C}." />   内置的转换货币的转换器  例如:还有 E,P,F?等

    还有1些时间的内置转换器,太多了,不想写了

   有 d,D,f,F,s,M,G

1
0
« 上一篇: SubSnoic 框架入门到提高(2)---全程记录
» 下一篇: 潜移默化学会设计模式-《1》-简单工厂模式《搭积木的例子》

1. binding 后面的stringFormat的写法----连接字符串

     <TextBlock Text="{Binding Path=Qty, StringFormat=Quantity: \{0\}}" />

2.

复制代码
    [ValueConversion(typeof(decimal), typeof(string))]
    public class PriceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            decimal price = (decimal)value;
            return price.ToString("c", culture);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string price = value.ToString();
            
            decimal result;
            if (Decimal.TryParse(price, System.Globalization.NumberStyles.Any, culture, out result))
            {
                return result;
            }
            return value;
        }
    }
复制代码

用法  你懂的

复制代码
<TextBox Margin="5" Grid.Row="2" Grid.Column="1">
          <TextBox.Text>
            <Binding Path="UnitCost">
              <Binding.Converter>
                <local:PriceConverter></local:PriceConverter>
              </Binding.Converter>              
            </Binding>
          </TextBox.Text>          
        </TextBox>
复制代码

3.条件式的值转换器

复制代码
 public class PriceToBackgroundConverter : IValueConverter
    {
        public decimal MinimumPriceToHighlight
        {
            get;
            set;
        }

        public Brush HighlightBrush
        {
            get;
            set;
        }

        public Brush DefaultBrush
        {
            get;
            set;
        }

        public object Convert(object value, Type targetType, object parameter,
          System.Globalization.CultureInfo culture)
        {
            decimal price = (decimal)value;
            if (price >= MinimumPriceToHighlight)
                return HighlightBrush;
            else
                return DefaultBrush;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
          System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
复制代码

用法

先引入命名空间

然后在需要转换器的窗体中定义资源

<Window.Resources>
</local:ImagePathConverter>
    <local:PriceToBackgroundConverter x:Key="PriceToBackgroundConverter"
      DefaultBrush="{x:Null}" HighlightBrush="Orange" MinimumPriceToHighlight="10">      
    </local:PriceToBackgroundConverter>
  </Window.Resources>

用资源

     <Border DataContext="{Binding ElementName=lstProducts, Path=SelectedItem}"
              Background="{Binding Path=UnitCost, Converter={StaticResource PriceToBackgroundConverter}}"              
           Padding="7" >

例如 这是一个图片地址转成图片资源的一个转换器

复制代码
 public class ImagePathConverter : IValueConverter
    {
        private string imageDirectory = Directory.GetCurrentDirectory();
        public string ImageDirectory
        {
            get { return imageDirectory; }
            set { imageDirectory = value; }
        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string imagePath = Path.Combine(ImageDirectory, (string)value);
            return new BitmapImage(new Uri(imagePath)); 
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException("The method or operation is not implemented.");
        }
    }
复制代码

引入命名空间 ,定义资源

    <local:ImagePathConverter x:Key="ImagePathConverter"></local:ImagePathConverter>

用资源

   <Image Source="{Binding Path=ProductImagePath, Converter={StaticResource ImagePathConverter}}"
                     Width="100"
                     ></Image>

4. 多值转换器

复制代码
  public class ValueInStockConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // Return the total value of all the items in stock.
            decimal unitCost = (decimal)values[0];
            int unitsInStock = (int)values[1];
            return unitCost * unitsInStock;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
复制代码

5.含参数的转换器,前台页面上的一个例子的写法

复制代码
  <TextBlock Grid.Column="1" HorizontalAlignment="Left" Width="60" ToolTip="{Binding Number}">
                        <TextBlock.Text>
                            <Binding Path="Number" Converter="{StaticResource lengthCut}">
                                <Binding.ConverterParameter>
                                    <sys:String>m</sys:String>
                                </Binding.ConverterParameter>
                            </Binding>
                        </TextBlock.Text>
                    </TextBlock>
复制代码

本例sys是先引入    命名空间的

xmlns:sys="clr-namespace:System;assembly=mscorlib" 

6.其他stringFormat

 例如

  <TextBlock Text="{Binding Date,StringFormat={}{0:MM/dd/yyyy}}" />

  还有很多简单的 字母直接表示的

     <TextBlock Text="{Binding UnitCost,StringFormat=The Value is {0:C}." />   内置的转换货币的转换器  例如:还有 E,P,F?等

    还有1些时间的内置转换器,太多了,不想写了

   有 d,D,f,F,s,M,G

猜你喜欢

转载自www.cnblogs.com/lonelyxmas/p/10459231.html
今日推荐