Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class StringToByteConverter : IValueConverter
- {
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
- {
- return value;
- }
- //hex string to byte array
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- if (value is String)
- {
- string valueTyped = (String)value;
- try
- {
- return Enumerable.Range(0, valueTyped.Length)
- .Where(x => x % 2 == 0)
- .Select(x => System.Convert.ToByte(valueTyped.Substring(x, 2), 16))
- .ToArray();
- }
- catch (Exception ex)
- {
- //clear the text box
- return new byte[] { 0x00 };
- }
- }
- return new byte[] {0x00};
- }
- }
- public class WProtocol
- {
- private byte[] _seq = new byte[1] { 0x00 };
- public byte[] Seq
- {
- get { return _seq; }
- set
- {
- _seq = value;
- }
- }
- }
- <Grid>
- <Label Content="Sequence :" HorizontalAlignment="Left" Height="24" Margin="29,24,0,0" VerticalAlignment="Top" Width="87"/>
- <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"/>
- <TextBox HorizontalAlignment="Left" Height="20" Margin="282,28,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="69"/>
- </Grid>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement