2016-07-12 1 views
0

ここでは、以下のコードのようにこのトグルボタンがあります。このトグルボタンにはcontrol templateも定義されています。状況によっては、このボタンが無効になっています。すなわち、ボタン上のI cannot click。私はMVVMパターンに従っています。私はブレークポイントを入れてチェックしました。ボタンIsEnabledの状態を変更することには何も関係しません。トグルボタンがどのように無効になっているのかわかりません

私はボタンの有効状態がどのように変化しているか把握しようとしています。ボタンの有効な状態の変更やそれに関連するものは何もありません。 したがって、ここでは不要なコードを表示/表示することはありません。

XAMLコードからボタンの状態がどのように無効に変更されているか、またはそのプロパティがその変更につながっているかを知ることができれば、非常に役に立ちます。前もって感謝します。

<ToggleButton x:Name="TestBtn" 
          Height="60" 
          Margin="5" 
          HorizontalAlignment="Stretch" 
          HorizontalContentAlignment="Center" 
          Command="{Binding StartStopResetCommand}" 
          Content="{Binding BtnState, 
              Converter={StaticResource BtnTextConverter}}" 
          FontSize="16" 
          Foreground="White" 
          PreviewKeyDown="BtnPreviewKeyDown"> 
       <ToggleButton.Style> 
        <Style TargetType="{x:Type ToggleButton}"> 
         <Setter Property="Template"> 
          <Setter.Value> 
           <ControlTemplate TargetType="{x:Type ToggleButton}"> 
            <Border x:Name="border" 
              Background="{TemplateBinding Background}" 
              BorderBrush="{TemplateBinding BorderBrush}" 
              BorderThickness=".5" 
              SnapsToDevicePixels="true"> 
             <ContentPresenter x:Name="contentPresenter" 
                  Margin="{TemplateBinding Padding}" 
                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                  Focusable="False" 
                  RecognizesAccessKey="True" 
                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> 
            </Border> 
            <ControlTemplate.Triggers> 
             <Trigger Property="IsEnabled" Value="false"> 
              <Setter TargetName="contentPresenter" Property="TextElement.Foreground" Value="#595959" /> 
             </Trigger> 
            </ControlTemplate.Triggers> 
           </ControlTemplate> 
          </Setter.Value> 
         </Setter> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding BtnState}" Value="{x:Static my:ButtonState.Start}"> 
           <Setter Property="Background" Value="{StaticResource ButtonBackgroundBrush}" /> 
          </DataTrigger> 
          <DataTrigger Binding="{Binding BtnState}" Value="{x:Static my:ButtonState.Stop}"> 
           <Setter Property="Background" Value="{StaticResource ExecutingStateColor}" /> 
          </DataTrigger> 
          <DataTrigger Binding="{Binding BtnState}" Value="{x:Static my:ButtonState.Reset}"> 
           <Setter Property="Background" Value="{StaticResource ButtonBackgroundBrush}" /> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </ToggleButton.Style> 
      </ToggleButton> 
+5

"StartStopResetCommand"アクションが "CanExecute"アクションを実装し、ある時点でfalseを返す場合、ボタンの状態は自動的に変更できます。 – Matas

+0

** Upvote。**とても速く正確です。私はまだそのようなことを知らなかった。ありがとう。 – ViVi

答えて

1

あなたStartStopResetCommandCanExecuteアクションを実装し、いくつかの点でfalseを返す場合、ボタンの状態は、この場合は無効になり、自動的に変更することができます。

さらに、すべてのコントロールがCanExecuteに応じて状態を更新するようにするには、コマンドにRaiseCanExecuteChangedを呼び出す必要があります。

+0

ありがとうございます。完璧に動作します。 – ViVi

関連する問題