Skip to content

Commit

Permalink
Support Copy In Lisstbox
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed May 16, 2022
1 parent 39cde45 commit 18d366b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
14 changes: 14 additions & 0 deletions NavisAddinManager/View/Control/LogControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<ListBox
x:Name="listBox_LogMessages"
FontFamily="{Binding DisplayFontFamily}"
SelectionMode="Extended"
ItemsSource="{Binding MessageList}"
>
<ListBox.ItemTemplate>
Expand All @@ -41,6 +42,19 @@
Text="{Binding Message}" />
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.CommandBindings>
<CommandBinding Command="ApplicationCommands.Copy"
CanExecute="CtrlCCopyCmdCanExecute" Executed="CtrlCCopyCmdExecuted" />
</ListBox.CommandBindings>
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Command="Copy">
<MenuItem.CommandBindings>
<CommandBinding Command="ApplicationCommands.Copy" CanExecute="RightClickCopyCmdCanExecute" Executed="RightClickCopyCmdExecuted" />
</MenuItem.CommandBindings>
</MenuItem>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
Expand Down
34 changes: 31 additions & 3 deletions NavisAddinManager/View/Control/LogControl.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Windows.Controls;
using System.Text;
using System.Windows.Input;
using NavisAddinManager.Model;
using NavisAddinManager.ViewModel;
using UserControl = System.Windows.Controls.UserControl;

Expand All @@ -17,8 +19,34 @@ public LogControl()
viewModel.FrmLogControl = this;
this.Loaded += viewModel.LogFileWatcher;
this.Unloaded += viewModel.UserControl_Unloaded;
}
private void RightClickCopyCmdExecuted(object sender, ExecutedRoutedEventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (LogMessageString dd in listBox_LogMessages.SelectedItems)
{
sb.AppendLine(dd.Message);
}
Clipboard.SetText(sb.ToString());
}
private void RightClickCopyCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = listBox_LogMessages.SelectedItem != null;
}
private void CtrlCCopyCmdExecuted(object sender, ExecutedRoutedEventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (LogMessageString dd in listBox_LogMessages.SelectedItems)
{
sb.AppendLine(dd.Message);
}
Clipboard.SetText(sb.ToString());
}

private void CtrlCCopyCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = listBox_LogMessages.SelectedItem != null;
}

}
}
}

0 comments on commit 18d366b

Please sign in to comment.