码迷,mamicode.com
首页 > Windows程序 > 详细

WPF之转换器Converter

时间:2018-05-12 11:17:59      阅读:1226      评论:0      收藏:0      [点我收藏+]

标签:parameter   tick   normal   int   minimum   span   icc   dde   mil   

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>
View Code

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     }
View Code

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     }
View Code

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     }
View Code

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     }
View Code

枚举

public enum LightType
    {
        [Description("关闭")]
        Close = 1,
        [Description("弱光")]
        Low = 2,
        [Description("中光")]
        Normal = 3,
        [Description("强光")]
        High = 4,
        [Description("爆闪")]
        Sharpflash = 5

    }

 

 

 

 

 

 

 

 

 

 

技术分享图片

 

WPF之转换器Converter

标签:parameter   tick   normal   int   minimum   span   icc   dde   mil   

原文地址:https://www.cnblogs.com/LY-HeroesRebor/p/9027671.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!