WPF之转换器Converter
Posted 逛园子$$$
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF之转换器Converter相关的知识,希望对你有一定的参考价值。
1、效果截图
拖动Slider,相应的灯光图片,字体颜色、大小都改变
2、前端代码
1 <Grid Height="224" Margin="0,561,0,205"> 2 <Image Stretch="Fill" Height="44" Source="/BirdDetect_Images/bar04.png" Margin="0,0,0,180"></Image> 3 <Label Content="灯光控制" Padding="15,8" Foreground="White" FontSize="18" FontFamily="Microsoft YaHei" Margin="0,0,0,180"></Label> 4 <Image Height="80" Width="80" Stretch="Fill" Source="{Binding BtnLight, Converter={StaticResource LightTypeToPicConverter} }" Margin="98,49,95,95"></Image> 5 <StackPanel Orientation="Horizontal" Margin="10,148,10,45"> 6 <TextBlock Text="关闭" TextAlignment="Center" FontSize="{Binding BtnLight, Converter={StaticResource LightSliderConverter},ConverterParameter=1}" FontFamily="Microsoft YaHei" Width="50" Foreground="{Binding BtnLight, Converter={StaticResource LightTypeToColorConverter},ConverterParameter=1}" ></TextBlock> 7 <TextBlock Text="弱光" TextAlignment="Center" FontSize="{Binding BtnLight, Converter={StaticResource LightSliderConverter},ConverterParameter=2}" FontFamily="Microsoft YaHei" Width="50" Foreground="{Binding BtnLight, Converter={StaticResource LightTypeToColorConverter},ConverterParameter=2}" ></TextBlock> 8 <TextBlock Text="中光" TextAlignment="Center" FontSize="{Binding BtnLight, Converter={StaticResource LightSliderConverter},ConverterParameter=3}" FontFamily="Microsoft YaHei" Width="50" Foreground="{Binding BtnLight, Converter={StaticResource LightTypeToColorConverter},ConverterParameter=3}" ></TextBlock> 9 <TextBlock Text="强光" TextAlignment="Center" FontSize="{Binding BtnLight, Converter={StaticResource LightSliderConverter},ConverterParameter=4}" FontFamily="Microsoft YaHei" Width="50" Foreground="{Binding BtnLight, Converter={StaticResource LightTypeToColorConverter},ConverterParameter=4}" ></TextBlock> 10 <TextBlock Text="爆闪" TextAlignment="Center" FontSize="{Binding BtnLight, Converter={StaticResource LightSliderConverter},ConverterParameter=5}" FontFamily="Microsoft YaHei" Width="50" Foreground="{Binding BtnLight, Converter={StaticResource LightTypeToColorConverter},ConverterParameter=5}" ></TextBlock> 11 </StackPanel> 12 <Slider x:Name="lightSlider" Value="{Binding BtnLight, Converter={StaticResource SliderToLightConverter}}" HorizontalAlignment="Left" Height="30" Margin="28,184,0,0" VerticalAlignment="Top" Width="216" Style="{DynamicResource LightSliderStyle2}" Maximum="5" Minimum="1" SmallChange="1" TickFrequency="1" IsSnapToTickEnabled="True" > 13 <i:Interaction.Triggers> 14 <i:EventTrigger EventName="LostMouseCapture"> 15 <Command:EventToCommand Command="{Binding SetSpeakerLightCmd}" /> 16 </i:EventTrigger> 17 </i:Interaction.Triggers> 18 </Slider> 19 <Image Stretch="Fill" Height="10" Source="/BirdDetect_Images/bar05.png" Margin="0,232,0,-18"></Image> 20 </Grid>
3、后端代码
1)、SliderToLightConverter
1 public class SliderToLightConverter : IValueConverter 2 { 3 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 4 { 5 LightType lt = (LightType)value; 6 switch (lt) 7 { 8 case (LightType)1: 9 return 1; 10 case (LightType)2: 11 return 2; 12 case (LightType)3: 13 return 3; 14 case (LightType)4: 15 return 4; 16 default: 17 return 5; 18 } 19 20 } 21 22 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 23 { 24 switch (int.Parse(value.ToString())) 25 { 26 case 1: 27 return (LightType)1; 28 case 2: 29 return (LightType)2; 30 case 3: 31 return (LightType)3; 32 case 4: 33 return (LightType)4; 34 default: 35 return (LightType)5; 36 } 37 } 38 }
2)、LightTypeToPicConverter
1 public class LightTypeToPicConverter:IValueConverter 2 { 3 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 4 { 5 LightType lt = (LightType)value; 6 switch (lt) 7 { 8 case (LightType)1: 9 return "/BirdDetect_Images/light1.png"; 10 case (LightType)2: 11 return "/BirdDetect_Images/light2.png"; 12 case (LightType)3: 13 return "/BirdDetect_Images/light3.png"; 14 case (LightType)4: 15 return "/BirdDetect_Images/light4.png"; 16 default: 17 return "/BirdDetect_Images/light5.png"; 18 } 19 20 21 } 22 23 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 24 { 25 throw new NotImplementedException(); 26 } 27 }
3)、LightSliderConverter
1 public class LightSliderValueConverter : IValueConverter 2 { 3 4 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 5 { 6 LightType lt1 = (LightType)value; 7 LightType lt2 = (LightType)int.Parse(parameter.ToString()); 8 if (lt1 == lt2 ) 9 { 10 return 18; 11 } 12 else 13 { 14 return 15; 15 } 16 } 17 18 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 19 { 20 throw new NotImplementedException(); 21 } 22 }
4)、LightTypeToColorConverter
1 public class LightTypeToColorConverter : IValueConverter 2 { 3 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 4 { 5 LightType lt1 = (LightType)value; 6 LightType lt2 = (LightType)int.Parse(parameter.ToString()); 7 if (lt1 == lt2) 8 { 9 return "#FF1546EE"; 10 } 11 else 12 { 13 return "White"; 14 } 15 } 16 17 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 18 { 19 throw new NotImplementedException(); 20 } 21 }
枚举
public enum LightType { [Description("关闭")] Close = 1, [Description("弱光")] Low = 2, [Description("中光")] Normal = 3, [Description("强光")] High = 4, [Description("爆闪")] Sharpflash = 5 }
以上是关于WPF之转换器Converter的主要内容,如果未能解决你的问题,请参考以下文章
WPF Binding值转换器ValueConverter使用简介
WPF Binding值转换器ValueConverter使用简介
spring jpa之实体属性类型转换器AttributeConverter,自定义Converter,通用Converter