Skip to content

Commit 0eff3de

Browse files
Merge pull request #15 from mujurin1/issue-10
コメントのコンテキストメニューのContext修正 Fixed #10
2 parents a8530aa + 96c939e commit 0eff3de

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

MultiCommentViewer/ViewModels/MainViewModel.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,9 @@ public bool ContainsUrl
13751375
}
13761376
private string GetUrlFromSelectedComment()
13771377
{
1378-
var selectedComment = SelectedComment;
1378+
// CopyComment と同じ理由
1379+
var commentDataGrid = GetActiveWindowContextAsCommentDataGrid();
1380+
var selectedComment = commentDataGrid.SelectedComment;
13791381
if (selectedComment == null)
13801382
{
13811383
return null;
@@ -1401,14 +1403,27 @@ private void OpenUrl()
14011403
}
14021404
private void CopyComment()
14031405
{
1404-
var message = SelectedComment.MessageItems.ToText();
1406+
// 本当はこのModel (this) が CommentDataGridViewModelBase を継承しているのでこんなのは不要だが
1407+
// CommentDataGrid 用のコードが UserView に無いのでこうなっている (中途半端な共通化の埋め合わせ)
1408+
var commentDataGrid = GetActiveWindowContextAsCommentDataGrid();
1409+
1410+
var message = commentDataGrid.SelectedComment.MessageItems.ToText();
14051411
try
14061412
{
14071413
System.Windows.Clipboard.SetText(message);
14081414
}
14091415
catch (System.Runtime.InteropServices.COMException) { }
14101416
SetSystemInfo("copy: " + message, InfoType.Debug);
14111417
}
1418+
1419+
private static CommentDataGridViewModelBase GetActiveWindowContextAsCommentDataGrid()
1420+
{
1421+
var activeWindow = Application.Current.Windows
1422+
.OfType<Window>()
1423+
.SingleOrDefault(x => x.IsActive);
1424+
1425+
return activeWindow.DataContext as CommentDataGridViewModelBase;
1426+
}
14121427
#region ConnectionsView
14131428
#region ConnectionsViewSelection
14141429
public int ConnectionsViewSelectionDisplayIndex

MultiCommentViewer/Views/CommentDataGrid.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="{Binding SelectedRowForeColor}" />
6161
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="{Binding SelectedRowBackColor}"/>
6262
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="{Binding SelectedRowForeColor}"/>
63-
<ContextMenu x:Key="commentContext" DataContext="{Binding DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}">
63+
<ContextMenu x:Key="commentContext" Opened="ContextMenu_Opened">
6464
<MenuItem Header="コメントをコピー" Command="{Binding CommentCopyCommand}" />
6565
<MenuItem Header="URLを開く" Command="{Binding OpenUrlCommand}" />
6666
<MenuItem Header="日本語に翻訳する" Command="{Binding TranslateCommand}" />

MultiCommentViewer/Views/CommentDataGrid.xaml.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ await _dispatcher.BeginInvoke((Action)(() =>
4646
};
4747
}
4848

49-
49+
private void ContextMenu_Opened(object sender, RoutedEventArgs e)
50+
{
51+
if (sender is ContextMenu contextMenu) {
52+
contextMenu.DataContext = Application.Current.MainWindow.DataContext;
53+
}
54+
}
5055

5156
public bool IsShowUserInfoMenuItem
5257
{

0 commit comments

Comments
 (0)