Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. public class StringToByteConverter : IValueConverter
  2. {
  3. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  4. {
  5. return value;
  6. }
  7.  
  8. //hex string to byte array
  9. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  10. {
  11. if (value is String)
  12. {
  13. string valueTyped = (String)value;
  14. try
  15. {
  16. return Enumerable.Range(0, valueTyped.Length)
  17. .Where(x => x % 2 == 0)
  18. .Select(x => System.Convert.ToByte(valueTyped.Substring(x, 2), 16))
  19. .ToArray();
  20. }
  21. catch (Exception ex)
  22. {
  23. //clear the text box
  24. return new byte[] { 0x00 };
  25. }
  26.  
  27. }
  28. return new byte[] {0x00};
  29. }
  30. }
  31.  
  32. public class WProtocol
  33. {
  34. private byte[] _seq = new byte[1] { 0x00 };
  35.  
  36. public byte[] Seq
  37. {
  38. get { return _seq; }
  39. set
  40. {
  41. _seq = value;
  42. }
  43. }
  44. }
  45.  
  46. <Grid>
  47. <Label Content="Sequence :" HorizontalAlignment="Left" Height="24" Margin="29,24,0,0" VerticalAlignment="Top" Width="87"/>
  48. <TextBox HorizontalAlignment="Left" Height="22" Margin="115,26,0,0" TextWrapping="Wrap" Text="{Binding Seq, Converter= {StaticResource StringToByteConverter},Mode=TwoWay, UpdateSourceTrigger=LostFocus}" VerticalAlignment="Top" Width="67" FontSize="9" FontFamily="Arial"/>
  49. <TextBox HorizontalAlignment="Left" Height="20" Margin="282,28,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="69"/>
  50. </Grid>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement