2017-09-07 20 views
1

WPFのボタンに問題があります。
私は多くのページと相対ボタン(> 50)を持つGUIを開発しています。
GUIはタッチスクリーンディスプレイで使用されるはずなので、スマートフォンアプリのどのボタンと同様に、各ボタンには2つのグラフィックス状態が押されていて正常です(ホバリング状態はありません)。関連するC#ファイルC#WPF - 2つの状態ボタンの問題。 (タッチイベント付きのクリックイベント)

private void buttonImageDown(object sender, RoutedEventArgs e) 
{ 
    Button button = sender as Button; 
    button.Content = new Image { 
     Source = new BitmapImage(new Uri(button.Tag + "")), 
     VerticalAlignment = VerticalAlignment.Center, 
     Stretch = Stretch.Uniform 
    }; 
} 

private void buttonImageUp(object sender, RoutedEventArgs e) 
{ 
    Button button = sender as Button; 
    button.Content = new Image { 
     Source = new BitmapImage(new Uri(button.Uid)), 
     VerticalAlignment = VerticalAlignment.Center, 
     Stretch = Stretch.Uniform 
    }; 
} 

これを内部

<Button 
    Uid=".../image_up.png" 
    Tag=".../image_down.png" 
    Style="{StaticResource flatButtonStyle}" 
    TouchLeave="buttonImageUp" 
    TouchDown="buttonImageDown" 
    Click="clickEvent" 
    ClickMode="Press" 
    > 
    <Button.Background> 
     <ImageBrush ImageSource=".../image_up.png" Stretch="Uniform"/> 
    </Button.Background> 
</Button> 

ページの一つで

リソースファイル

<Application.Resources> 
    <Style x:Key="flatButtonStyle" TargetType="Button"> 
     <Setter Property="OverridesDefaultStyle" Value="True"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Button"> 
        <Border Name="border" 
         Background="{TemplateBinding Background}"> 
         <ContentPresenter HorizontalAlignment="Left" VerticalAlignment="Center" /> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsMouseOver" Value="True"> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Application.Resources> 

:現在、私はここに、このソリューションを使用してい
implementatinには多くの問題があります:

私はそれがタッチ

  • あたり2時間をC#の機能を実行するために持っているので、私はC#関数に私がする必要があるたびにコピーする必要がUIDを使用して

  • 遅い応答タグを付けることはできません

    • 新しいページのボタンを使用する

    • しかし実際の問題は、ボタンが押されたときにclickEventが起動しない理由がわからないことです。また、一部のコンピュータではclickEventは最初の3〜 touchDonwとtouchLeaveの5倍は...

    (Iは、.NET 4.6.1しかし4.5.2及び4.7と同じ動作を使用している)と思うC#の成分を除去し、ボタンの画像(ソース)を変更することが可能ですちょうどXAMLで、しかし私はしばらく探して、私は何も見つけることができません。

    誰かお手伝いできますか?

    <Style x:Key="flatButtonStyle" TargetType="{x:Type Button}"> 
        <Setter Property="OverridesDefaultStyle" Value="True" /> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate TargetType="Button"> 
    
           <Border BorderBrush="{TemplateBinding BorderBrush}" 
             BorderThickness="{TemplateBinding BorderThickness}" 
             Background="{TemplateBinding Background}" 
             Padding="{TemplateBinding Padding}"> 
    
            <ContentPresenter TextBlock.Foreground="{TemplateBinding Foreground}" 
                 HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                 VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
           </Border> 
    
           <ControlTemplate.Triggers> 
            <Trigger Property="IsEnabled" Value="False"> 
             <!-- Setters to style disabled state --> 
            </Trigger> 
    
            <Trigger Property="IsPressed" Value="True"> 
             <!-- Setters to style pressed state --> 
            </Trigger> 
           </ControlTemplate.Triggers> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
    </Style> 
    

    あなたがあなた自身のスタイルを適用するために持っていて、関連propetiesに触れIsPressedを変更する必要がありますが、これはトリックを行う必要がありpossiblityがある:

  • +1

    [ToggleButton](https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton(v = vs.110).aspx)をエミュレートしようとしていますか? – icebat

    +0

    イメージを設定するためにスタイルトリガーでこれを行うことはできませんか? UidとTagの問題は何ですか? –

    +0

    @icebatはいですが、C#を使用することはあまり効率的ではありません。 – xSanciopanzax

    答えて

    1

    はこのような何かを試してみてください。 C#コードでカスタムロジックが不要です。

    +0

    私はこれを試してみてください。 'C ' ' これはマウスでは機能しません。「IsTouchDown」や「IsTouchedLeave」のようなものはありません。 また、このようにして、すべてのボタンのスタイルを作成する必要があります。 – xSanciopanzax

    +0

    私はタッチスクリーン上にアプリを開発していませんが、タッチしたボタンがそれを押しているので、この場合はIsPressedで十分だと思います。私はどのように私の例を使用したかは分かりませんが、テンプレートを設定する間、ControlTemplateタグは必須です。質問を編集し、書式設定されたコードを貼り付けて、自分のソリューションをどのように実装したかを表示できますか? また、ボタンごとにスタイルを作成する必要はありません。 Application.Resourcesや他のリソース辞書に作成し、以前と同じように使用してください。 –

    関連する問題