質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

2回答

2986閲覧

ContextMenuのハイライト非表示

dn315

総合スコア201

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

0クリップ

投稿2017/05/04 07:55

###前提・実現したいこと

こんにちは

WPFでボタン押すとContextMenuの中にカレンダーが表示され、
年月を選択できる物を作っています。

カレンダー選択まではうまくいったのですが、
カレンダーの上にカーソルを持っていくと出てくる
青いハイライトがどうしても消えてくれません。

ContextMenuのIsMouseOverTriggerや
FocusVisualStyleをnullにしましたがダメでした。

宜しくお願いします。

###発生している問題

イメージ説明

###該当のソースコード

XAML

1<Window x:Class="WpfApplication1.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApplication1" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="200" Width="300"> 9 10 <Window.Resources> 11 <local:MainWindowViewModel x:Key="ViewModel"/> 12 13 <Style x:Key="CalendarButton" TargetType="{x:Type Button}"> 14 <Setter Property="ContextMenu"> 15 <Setter.Value> 16 <ContextMenu> 17 <ContextMenu.Template> 18 <ControlTemplate> 19 <Border Name="border"> 20 <!--<Border.Style> 21 <Style TargetType="Border"> 22 <Setter Property="Margin" Value="-34,-2,-49,-2"/> 23 </Style> 24 </Border.Style>--> 25 <ItemsPresenter/> 26 </Border> 27 </ControlTemplate> 28 </ContextMenu.Template> 29 <Calendar x:Name="calendar1" DisplayMode="Year" Visibility="Hidden"/> 30 </ContextMenu> 31 </Setter.Value> 32 </Setter> 33 </Style> 34 </Window.Resources> 35 <Window.DataContext> 36 <StaticResource ResourceKey="ViewModel"/> 37 </Window.DataContext> 38 39 <Grid> 40 <Button 41 Width="150" 42 Height="40" 43 Style="{StaticResource CalendarButton}" 44 Command="{Binding ButtonClickCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}"> 45 <Button.InputBindings> 46 <MouseBinding Gesture="RightClick" Command="{Binding RightClickDisableCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=Button,AncestorLevel=1}}"/> 47 </Button.InputBindings> 48 <StackPanel Orientation="Horizontal"> 49 <!--<Image Source="/img/calendar.png" Width="24" Height="24" Stretch="Fill"/>--> 50 <TextBlock 51 Width="85" 52 Text="{Binding SelectedDate,Mode=OneWay,StringFormat={}{0:yyyy年MM月},UpdateSourceTrigger=PropertyChanged}" 53 VerticalAlignment="Center" 54 Margin="5,0,0,0"/> 55 </StackPanel> 56 </Button> 57 </Grid> 58</Window>

C#

1using System; 2using System.ComponentModel; 3using System.Runtime.CompilerServices; 4using System.Windows.Controls; 5using System.Windows.Controls.Primitives; 6using System.Windows.Input; 7 8namespace WpfApplication1 9{ 10 public class MainWindowViewModel : INotifyPropertyChanged 11 { 12 13 /// <summary> 14 /// 選択された日付 15 /// </summary> 16 public DateTime SelectedDate 17 { 18 get { return _selectedDate; } 19 set 20 { 21 _selectedDate = value; 22 NotifyPropertyChanged(); 23 } 24 } 25 private DateTime _selectedDate = DateTime.Now.Date; 26 27 /// <summary> 28 /// ContextMenuの表示とカレンダーの選択コマンド 29 /// </summary> 30 public ICommand ButtonClickCommand { get; private set; } 31 32 /// <summary> 33 /// 右クリック時のContextMenuを表示禁止コマンド 34 /// </summary> 35 public ICommand RightClickDisableCommand { get; private set; } 36 37 /// <summary> 38 /// コンストラクタ 39 /// </summary> 40 public MainWindowViewModel() 41 { 42 this.ButtonClickCommand = new ButtonClick(); 43 this.RightClickDisableCommand = new RightClickDisable(); 44 } 45 46 /// <summary> 47 /// RightClickDisableCommand実装 48 /// </summary> 49 private class RightClickDisable : ICommand 50 { 51 public event EventHandler CanExecuteChanged 52 { 53 add { CommandManager.RequerySuggested += value; } 54 remove { CommandManager.RequerySuggested -= value; } 55 } 56 57 public bool CanExecute(object parameter) { return true; } 58 59 public void Execute(object parameter) 60 { 61 var button = parameter as Button; 62 button.ContextMenu.Visibility = System.Windows.Visibility.Hidden; 63 } 64 } 65 66 /// <summary> 67 /// ButtonClickCommand実装 68 /// </summary> 69 private class ButtonClick : ICommand 70 { 71 public event EventHandler CanExecuteChanged 72 { 73 add { CommandManager.RequerySuggested += value; } 74 remove { CommandManager.RequerySuggested -= value; } 75 } 76 77 public bool CanExecute(object parameter) { return true; } 78 79 public void Execute(object parameter) 80 { 81 var button = parameter as Button; 82 button.ContextMenu.Visibility = System.Windows.Visibility.Visible; 83 button.ContextMenu.PlacementTarget = button; 84 button.ContextMenu.Placement = PlacementMode.Bottom; 85 86 var cal = button.ContextMenu.Items[0] as Calendar; 87 button.ContextMenu.IsOpen = true; 88 cal.DisplayMode = CalendarMode.Month; 89 cal.DisplayMode = CalendarMode.Year; 90 cal.Visibility = System.Windows.Visibility.Visible; 91 92 cal.DisplayModeChanged += Calendar_DisplayModeChanged; 93 button.ContextMenu.Closed += ContextMenu_Closed; 94 } 95 96 private void ContextMenu_Closed(object sender, System.Windows.RoutedEventArgs e) 97 { 98 var contextMenu = sender as ContextMenu; 99 var cal = contextMenu.Items[0] as System.Windows.Controls.Calendar; 100 101 cal.DisplayModeChanged -= Calendar_DisplayModeChanged; 102 contextMenu.Closed -= ContextMenu_Closed; 103 } 104 105 private void Calendar_DisplayModeChanged(object sender, CalendarModeChangedEventArgs e) 106 { 107 var cal = sender as Calendar; 108 var vm = cal.DataContext as MainWindowViewModel; 109 110 if (cal.DisplayMode == CalendarMode.Month) 111 { 112 var contextMenu = cal.Parent as ContextMenu; 113 vm.SelectedDate = cal.DisplayDate; 114 cal.Visibility = System.Windows.Visibility.Hidden; 115 contextMenu.IsOpen = false; 116 } 117 } 118 } 119 120 #region PropertyChanged実装 121 122 public event PropertyChangedEventHandler PropertyChanged; 123 124 public void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 125 { 126 if (PropertyChanged != null) 127 { 128 PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 129 } 130 } 131 132 #endregion 133 134 } 135} 136

###試したこと
Marginを使って調整する事はできましたが
他の方法を知りたいです。

###補足情報(言語/FW/ツール等のバージョンなど)
VisualStudio2015Pro Update3
.Net Framework 4.5.2

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

コード貼っておきます

XAML

1<Window x:Class="WpfApplication1.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApplication1" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="200" Width="360"> 9 10 <Window.Resources> 11 <local:MainWindowViewModel x:Key="ViewModel"/> 12 </Window.Resources> 13 <Window.DataContext> 14 <StaticResource ResourceKey="ViewModel"/> 15 </Window.DataContext> 16 17 <Grid> 18 <StackPanel Orientation="Vertical" Width="150" Height="40"> 19 <ToggleButton 20 x:Name="calendarButton" 21 Height="{Binding Height,RelativeSource={RelativeSource FindAncestor,AncestorLevel=1,AncestorType={x:Type StackPanel}}}" 22 Command="{Binding ButtonClickCommand}" 23 CommandParameter="{Binding RelativeSource={RelativeSource Self}}"> 24 <StackPanel Orientation="Horizontal"> 25 <!--<Image Source="/img/calendar.png" Width="24" Height="24" Stretch="Fill"/>--> 26 <TextBlock 27 Width="85" 28 Text="{Binding SelectedDate,StringFormat={}{0:yyyy年MM月},UpdateSourceTrigger=PropertyChanged}" 29 VerticalAlignment="Center" 30 Margin="5,0,0,0"/> 31 </StackPanel> 32 </ToggleButton> 33 <Popup x:Name="calendarButtonPopup" IsOpen="{Binding IsChecked,ElementName=calendarButton}" PlacementTarget="{Binding ElementName=calendarButton}" Placement="Bottom"> 34 <Calendar x:Name="calendar1" Margin="0,-3,0,-3"/> 35 </Popup> 36 </StackPanel> 37 </Grid> 38</Window>

C#

1using System; 2using System.ComponentModel; 3using System.Runtime.CompilerServices; 4using System.Windows.Controls; 5using System.Windows.Controls.Primitives; 6using System.Windows.Input; 7 8namespace WpfApplication23 9{ 10 public class MainWindowViewModel : INotifyPropertyChanged 11 { 12 13 /// <summary> 14 /// 選択された日付 15 /// </summary> 16 public DateTime SelectedDate 17 { 18 get { return _selectedDate; } 19 set 20 { 21 _selectedDate = value; 22 NotifyPropertyChanged(); 23 } 24 } 25 private DateTime _selectedDate = DateTime.Now.Date; 26 27 /// <summary> 28 /// ContextMenuの表示とカレンダーの選択コマンド 29 /// </summary> 30 public ICommand ButtonClickCommand { get; private set; } 31 32 /// <summary> 33 /// コンストラクタ 34 /// </summary> 35 public MainWindowViewModel() 36 { 37 this.ButtonClickCommand = new ButtonClick(); 38 } 39 40 /// <summary> 41 /// ButtonClickCommand実装 42 /// </summary> 43 private class ButtonClick : ICommand 44 { 45 public event EventHandler CanExecuteChanged 46 { 47 add { CommandManager.RequerySuggested += value; } 48 remove { CommandManager.RequerySuggested -= value; } 49 } 50 51 public bool CanExecute(object parameter) { return true; } 52 53 public void Execute(object parameter) 54 { 55 var button = parameter as ToggleButton; 56 var popup = button.FindName("calendarButtonPopup") as Popup; 57 var cal = popup.FindName("calendar1") as Calendar; 58 59 cal.DisplayMode = CalendarMode.Year; 60 cal.DisplayModeChanged -= Calendar_DisplayModeChanged; 61 cal.DisplayModeChanged += Calendar_DisplayModeChanged; 62 } 63 64 private void Calendar_DisplayModeChanged(object sender, CalendarModeChangedEventArgs e) 65 { 66 var cal = sender as Calendar; 67 68 if (cal.DisplayMode == CalendarMode.Month) 69 { 70 (cal.DataContext as MainWindowViewModel).SelectedDate = cal.DisplayDate; 71 (cal.Parent as Popup).IsOpen = false; 72 cal.DisplayModeChanged -= Calendar_DisplayModeChanged; 73 } 74 } 75 } 76 77 #region PropertyChanged実装 78 79 public event PropertyChangedEventHandler PropertyChanged; 80 81 public void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 82 { 83 if (PropertyChanged != null) 84 { 85 PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 86 } 87 } 88 89 #endregion 90 91 } 92} 93

投稿2017/05/08 00:48

編集2017/05/08 02:17
dn315

総合スコア201

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

ContextMenu は MenuItem を並べて Popup するものです。
その用途には Popupコントロール を使って下さい。
使い方は ContextMenu ににてますがもっと簡単です。

投稿2017/05/06 09:55

hihijiji

総合スコア4150

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

dn315

2017/05/08 00:29

Popupで望み通り簡単にできました。 ご回答ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問