右键菜单

This commit is contained in:
陈国伟 2021-11-02 14:34:40 +08:00
parent a51b2bd807
commit 53e907d145
3 changed files with 28 additions and 2 deletions

View File

@ -8,6 +8,7 @@
d:DesignHeight="450" d:DesignWidth="800"> d:DesignHeight="450" d:DesignWidth="800">
<Grid> <Grid>
<ListBox <ListBox
x:Name="listBox"
ItemsSource="{Binding LogList}" ItemsSource="{Binding LogList}"
VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.IsVirtualizing="True"
ScrollViewer.CanContentScroll="True" ScrollViewer.CanContentScroll="True"
@ -16,7 +17,13 @@
Background="#f7f7f7"> Background="#f7f7f7">
<ListBox.ItemTemplate> <ListBox.ItemTemplate>
<DataTemplate> <DataTemplate>
<StackPanel Margin="0 5" > <StackPanel Margin="0 5">
<StackPanel.ContextMenu>
<ContextMenu>
<MenuItem Header="复制本项" Click="MenuItem_Copy_Click" />
<MenuItem Header="清除所有" Click="MenuItem_Clear_Click" />
</ContextMenu>
</StackPanel.ContextMenu>
<TextBlock TextWrapping="Wrap" Text="{Binding Timestamp, StringFormat={}{0:yyyy-MM-dd HH:mm:ss.fff}}"/> <TextBlock TextWrapping="Wrap" Text="{Binding Timestamp, StringFormat={}{0:yyyy-MM-dd HH:mm:ss.fff}}"/>
<TextBlock TextWrapping="Wrap" FontSize="12" Margin="0 5" Text="{Binding SourceContext}"/> <TextBlock TextWrapping="Wrap" FontSize="12" Margin="0 5" Text="{Binding SourceContext}"/>
<TextBlock TextWrapping="Wrap" FontSize="14" FontWeight="Light" Text="{Binding Message}"> <TextBlock TextWrapping="Wrap" FontSize="14" FontWeight="Light" Text="{Binding Message}">

View File

@ -41,5 +41,18 @@ namespace FastGithub.UI
this.Dispatcher.Invoke(() => this.LogList.Add(log)); this.Dispatcher.Invoke(() => this.LogList.Add(log));
this.BeginReceiveFrom(); this.BeginReceiveFrom();
} }
private void MenuItem_Copy_Click(object sender, System.Windows.RoutedEventArgs e)
{
if (this.listBox.SelectedValue is UdpLog udpLog)
{
udpLog.SetToClipboard();
}
}
private void MenuItem_Clear_Click(object sender, System.Windows.RoutedEventArgs e)
{
this.LogList.Clear();
}
} }
} }

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Windows;
using System.Windows.Media; using System.Windows.Media;
namespace FastGithub.UI namespace FastGithub.UI
@ -14,5 +15,10 @@ namespace FastGithub.UI
public string SourceContext { get; set; } public string SourceContext { get; set; }
public string Color => this.Level == "Information" ? "#333" : "IndianRed"; public string Color => this.Level == "Information" ? "#333" : "IndianRed";
}
public void SetToClipboard()
{
Clipboard.SetText($"{this.Timestamp.ToString("yyyy-MM-dd HH:mm:ss.fff")}\r\n{this.SourceContext}\r\n{this.Message}");
}
}
} }