前提
wpfを始めてから2か月の初心者です。
wpfのカスタムコントロールでタッチパネルで使用する画像ボタンを作っています。
タッチ時とディスエーブル時に画像を変化させます。
マウスでクリックするとすぐに画像が切り替わるのですが、指でタッチすると
画像が切り替わるまでに0.5秒ぐらいかかってしまいます。
wpf visualstudio2022 .netFramework4.8
実現したいこと
指でタッチした場合もマウスでクリックした時と同じように、
タッチした瞬間に画像を切り替えたい。
発生している問題・エラーメッセージ
エラーメッセージ
該当のソースコード
Generic.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MenuDisplay">
</ResourceDictionary><Style TargetType="{x:Type local:OriginalButton}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:OriginalButton}"> <Grid Stylus.IsPressAndHoldEnabled="False"> <Image Name="image" Source="{TemplateBinding EnableImage}"> </Image> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsPressed" Value="True" > <Setter TargetName="image" Property="Source" Value="{Binding ClickImage, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type local:OriginalButton}}}"/> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter TargetName="image" Property="Source" Value="{Binding DisableImage, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:OriginalButton}}}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
OriginalButton.cs
public class OriginalButton : Button { static OriginalButton() { DefaultStyleKeyProperty.OverrideMetadata(typeof(OriginalButton), new FrameworkPropertyMetadata(typeof(OriginalButton))); } public ImageSource EnableImage { get { return (ImageSource)GetValue(EnableImageProperty); } set { SetValue(EnableImageProperty, value); } } public static readonly DependencyProperty EnableImageProperty = DependencyProperty.Register("EnableImage", typeof(ImageSource), typeof(OriginalButton), new PropertyMetadata(null)); public ImageSource DisableImage { get { return (ImageSource)GetValue(DisableImageProperty); } set { SetValue(DisableImageProperty, value); } } public static readonly DependencyProperty DisableImageProperty = DependencyProperty.Register("DisableImage", typeof(ImageSource), typeof(OriginalButton), new PropertyMetadata(null)); public ImageSource ClickImage { get { return (ImageSource)GetValue(ClickImageProperty); } set { SetValue(ClickImageProperty, value); } } public static readonly DependencyProperty ClickImageProperty = DependencyProperty.Register("ClickImage", typeof(ImageSource), typeof(OriginalButton), new PropertyMetadata(null)); }
MainWindow.xaml
<Window x:Class="MenuDisplay.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MenuDisplay"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<local:OriginalButton x:Name="MenuButton" Content="OriginalButton"
HorizontalAlignment="Center" VerticalAlignment="Center"
EnableImage="Images\A.png"
ClickImage="Images\B.png"
DisableImage="Images\C.PNG"
/>
</Grid>
</Window>
### 試したこと 当初はマウスクリックで画像が切り替わるのにタッチでは切り替わらなかったのですが、 Stylus.IsPressAndHoldEnabled="False"を追加することでタッチにより画像切り替えが 可能となりました。 しかし、マウスクリック時に比べて明らかにタッチでの画像切り替えが遅いです。 Stylus.IsPressAndHoldEnabled="False"の設定の仕方が悪いのか 根本的にアプローチの仕方が悪いのか分かりません。 ### 補足情報(FW/ツールのバージョンなど) ここにより詳細な情報を記載してください。

回答1件
あなたの回答
tips
プレビュー