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

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

新規登録して質問してみよう
ただいま回答率
85.46%
WPF

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

Q&A

解決済

1回答

1812閲覧

WPF:ドロップダウンメニュー内でマウスオーバー時に背景色変更を無効にしたいです

sarupar

総合スコア2

WPF

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

1グッド

0クリップ

投稿2021/09/14 08:00

dropdownメニュー内の背景色(マウスオーバー時)について、教えて頂きたく
質問させていただきました。
WPFで以下のようにdropdown内にボタンを配置しました。
dropdownを表示してボタン表示は出来ているのですが、
マウスオーバー時、その行全体の背景色がついてしまいます。
マウスオーバーしても行の背景色を変更したくないのですが、
これは可能でしょうか?
WPF学習中の初心者で申し訳ありませんが、
よろしくお願いします。

XAML

1<Window 2 x:Class="WpfDwopDownMenuButton.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:l="clr-namespace:WpfDwopDownMenuButton" 6 Title="DwopDownMenuButton Test" Height="100" Width="200"> 7 8 9 <Grid> 10 <l:DropDownMenuButton Width="120" Height="32"> 11 12 <TextBlock Text="Show menu" /> 13 14 <l:DropDownMenuButton.DropDownContextMenu> 15 <ContextMenu> 16 <Button Content="AAAA"/> 17 <Button Content="BBBB"/> 18 </ContextMenu> 19 </l:DropDownMenuButton.DropDownContextMenu> 20 </l:DropDownMenuButton> 21 </Grid> 22</Window>

C#

1using System.Windows; 2using System.Windows.Controls; 3using System.Windows.Controls.Primitives; 4using System.Windows.Data; 5 6namespace WpfDwopDownMenuButton 7{ 8 /// <summary> 9 /// ドロップ ダウン メニューを表示する為のボタン コントロール クラスです。 10 /// </summary> 11 public sealed class DropDownMenuButton : ToggleButton 12 { 13 /// <summary> 14 /// インスタンスを初期化します。 15 /// </summary> 16 public DropDownMenuButton() 17 { 18 var binding = new Binding("DropDownContextMenu.IsOpen") { Source = this }; 19 this.SetBinding(DropDownMenuButton.IsCheckedProperty, binding); 20 } 21 22 /// <summary> 23 /// ドロップ ダウンとして表示するコンテキスト メニューを取得または設定します。 24 /// </summary> 25 public ContextMenu DropDownContextMenu 26 { 27 get 28 { 29 return this.GetValue(DropDownContextMenuProperty) as ContextMenu; 30 } 31 set 32 { 33 this.SetValue(DropDownContextMenuProperty, value); 34 } 35 } 36 37 /// <summary> 38 /// コントロールがクリックされた時のイベントです。 39 /// </summary> 40 protected override void OnClick() 41 { 42 if (this.DropDownContextMenu == null) { return; } 43 44 this.DropDownContextMenu.PlacementTarget = this; 45 this.DropDownContextMenu.Placement = PlacementMode.Bottom; 46 this.DropDownContextMenu.IsOpen = !DropDownContextMenu.IsOpen; 47 } 48 49 /// <summary> 50 /// ドロップ ダウンとして表示するメニューを表す依存プロパティです。 51 /// </summary> 52 public static readonly DependencyProperty DropDownContextMenuProperty = DependencyProperty.Register("DropDownContextMenu", typeof(ContextMenu), typeof(DropDownMenuButton), new UIPropertyMetadata(null)); 53 } 54}
TN8001👍を押しています

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

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

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

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

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

guest

回答1

0

ベストアンサー

参考コードがある場合は出典を質問に明示してください。
WPF で DropDown メニューボタン - アカベコマイリ

中に入っているのがボタンなのは、意図してやっているということでいいんでしょうか?(すごい違和感があるのですが)

ボタンでいいならこんな感じでしょうか。

xml

1<Window 2 x:Class="Questions359466.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:l="clr-namespace:Questions359466" 6 Width="400" 7 Height="200"> 8 <StackPanel 9 HorizontalAlignment="Center" 10 VerticalAlignment="Center" 11 Orientation="Horizontal"> 12 13 <l:DropDownMenuButton MinWidth="120" Content="normal"> 14 <l:DropDownMenuButton.DropDownContextMenu> 15 <ContextMenu> 16 <Button Content="AAAA" /> 17 <Button Content="BBBB" /> 18 </ContextMenu> 19 </l:DropDownMenuButton.DropDownContextMenu> 20 </l:DropDownMenuButton> 21 22 <l:DropDownMenuButton MinWidth="120" Content="no highlight"> 23 <l:DropDownMenuButton.DropDownContextMenu> 24 <ContextMenu> 25 <ContextMenu.Resources> 26 <Style TargetType="{x:Type MenuItem}"> 27 <Setter Property="Template"> 28 <Setter.Value> 29 <ControlTemplate TargetType="{x:Type MenuItem}"> 30 <ContentPresenter ContentSource="Header" /> 31 </ControlTemplate> 32 </Setter.Value> 33 </Setter> 34 </Style> 35 </ContextMenu.Resources> 36 <Button Content="AAAA" /> 37 <Button Content="BBBB" /> 38 </ContextMenu> 39 </l:DropDownMenuButton.DropDownContextMenu> 40 </l:DropDownMenuButton> 41 </StackPanel> 42</Window>

投稿2021/09/14 09:19

編集2023/07/29 05:15
TN8001

総合スコア9401

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

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

sarupar

2021/09/14 11:04

現在、WPF学習中で、dropDownボタンの中に、意図的にボタンを配置してみよう、と試行(本来ならおかしいかもしれませんが)し、その中で、背景色を無くすことは出来るか、という疑問で質問させていただきました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問