implemented send on enter,

fixed selection of textbox in MultiFormatDataView
master
Jonas Arnold 3 years ago
parent 8abb83345c
commit e7164953bb
  1. 9
      MultiTerm.Core/ViewModel/CommunicationDataViewModel.cs
  2. 13
      MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.cs
  3. 21
      MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.cs
  4. 10
      MultiTerm.Wpf/View/SendReceiveView.xaml

@ -12,7 +12,6 @@ namespace MultiTerm.Core.ViewModel;
public partial class CommunicationDataViewModel : ObservableObject, ICommunicationDataViewModel<ByteDataViewModel, ExtendedByte>
{
private readonly IContext uiContext;
private int dataCharacterCount = 0;
private List<byte>? listOfPreviousCharacters = null;
@ -32,6 +31,10 @@ public partial class CommunicationDataViewModel : ObservableObject, ICommunicati
#endregion
/// <summary>
/// Newline Sequence string inserted on every newline in the <see cref="DataAsString"/>.
/// </summary>
public static string NewlineSequence = Environment.NewLine;
public CommunicationDataViewModel(IContext context)
{
@ -134,7 +137,7 @@ public partial class CommunicationDataViewModel : ObservableObject, ICommunicati
// increase line count
lineCounter++;
// append line in string
stringBuilder.AppendLine();
stringBuilder.Append(NewlineSequence);
break;
case ShouldIntroduceNewlineAfterThisByteResult.RequiresMoreCharacters:
@ -198,7 +201,7 @@ public partial class CommunicationDataViewModel : ObservableObject, ICommunicati
// increase line count
collectionLineCounter++;
// append line in string
stringBuilder.AppendLine();
stringBuilder.Append(NewlineSequence);
break;
case ShouldIntroduceNewlineAfterThisByteResult.RequiresMoreCharacters:

@ -277,15 +277,22 @@ public class MultiFormatDataView : Control
int selectionStartIndex = this.tbCharOnlyView!.SelectionStart;
// TEMP OLD
// extract text from the beginning to the start of the selected text
// var textFromBeginningToStartOfSelection = this.tbCharOnlyView!.Text.Substring(0, selectionStartIndex);
var textFromBeginningToStartOfSelection = this.tbCharOnlyView!.Text.Substring(0, selectionStartIndex);
// count amount of manually introduced newline sequences in this text section (these to not exist in the data source!)
var foundManuallyIntroducedNewlineSequences = 0; // = textFromBeginningToStartOfSelection.Count((x) => x == DataViewModelToStringConverter.NewlineSequence);
var foundManuallyIntroducedNewlineSequenceCharacters = 0;
var foundAmountOfLines = textFromBeginningToStartOfSelection.Split(CommunicationDataViewModel.NewlineSequence).Length;
// any newline sequences introduced (more than one lines found)
if (foundAmountOfLines > 1)
{
// calculated amount of characters that were introduced
foundManuallyIntroducedNewlineSequenceCharacters = (foundAmountOfLines - 1) * CommunicationDataViewModel.NewlineSequence.Length;
}
// iterate through length of selection
for (int i = 0; i < this.tbCharOnlyView!.SelectionLength; i++)
{
// subtracting the counted newline sequences and adding i (length)
int elementPositionInCollection = selectionStartIndex - foundManuallyIntroducedNewlineSequences + i;
int elementPositionInCollection = selectionStartIndex - foundManuallyIntroducedNewlineSequenceCharacters + i;
// add element to new selection list
newSelection.Add(this.DataSource.Data.ElementAt(elementPositionInCollection));
// next item does not exist => break loop

@ -43,12 +43,24 @@ public class MultiFormatTextBox : Control
#endregion
#region Dependency Properties
public static readonly DependencyProperty CurrentMultiFormatStringProperty =
DependencyProperty.Register("CurrentMultiFormatString",
typeof(MultiFormatString), typeof(MultiFormatTextBox),
new PropertyMetadata(null, OnCurrentMultiFormatStringChanged));
public static readonly RoutedEvent EnterPressedEvent;
/// <summary>
/// .NET Property for <see cref="EnterPressedEvent"/>
/// </summary>
public event RoutedEventHandler EnterPressed
{
add { this.AddHandler(EnterPressedEvent, value); }
remove { this.RemoveHandler(EnterPressedEvent, value); }
}
/// <summary>
/// .NET Property for CurrentMultiFormatString.
/// </summary>
@ -63,6 +75,11 @@ public class MultiFormatTextBox : Control
static MultiFormatTextBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MultiFormatTextBox), new FrameworkPropertyMetadata(typeof(MultiFormatTextBox)));
EnterPressedEvent = EventManager.RegisterRoutedEvent("EnterPressed",
RoutingStrategy.Bubble, typeof(RoutedEventArgs),
typeof(MultiFormatTextBox));
}
public override void OnApplyTemplate()
@ -264,8 +281,10 @@ public class MultiFormatTextBox : Control
{
if (e.Key == Key.Enter)
{
// TODO Raise EnterPressedEvent Here
e.Handled = true;
// raise event
RoutedEventArgs args = new(EnterPressedEvent);
RaiseEvent(args);
}
else if(e.Key == Key.Space)
{

@ -70,7 +70,15 @@
<!-- Send text box -->
<DockPanel DockPanel.Dock="Top" LastChildFill="True">
<Button DockPanel.Dock="Right" Content="Send" Command="{Binding SendCommand}"/>
<custom_controls:MultiFormatTextBox DockPanel.Dock="Left" CurrentMultiFormatString="{Binding Path=SendableData, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></custom_controls:MultiFormatTextBox>
<custom_controls:MultiFormatTextBox x:Name="sendTextBox" DockPanel.Dock="Left"
CurrentMultiFormatString="{Binding Path=SendableData, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<behaviors:Interaction.Triggers>
<behaviors:EventTrigger EventName="EnterPressed" SourceObject="{Binding ElementName=sendTextBox}">
<behaviors:InvokeCommandAction Command="{Binding SendCommand}" />
</behaviors:EventTrigger>
</behaviors:Interaction.Triggers>
</custom_controls:MultiFormatTextBox>
</DockPanel>
<Separator/>
<TextBox DockPanel.Dock="Bottom" Text="{Binding TempSentDataString}">

Loading…
Cancel
Save