0

ボタンが押されると、ボタンの背景 ( Rectangle Fill) が変わります。そのため、ユーザーはどのボタンが押され、何が押されていないかを確認できます。

問題:

トリガーと を使用してTogglebuttonを実行し"IsChecked"、背景を変更します。ただし、背景の変更は 1 回しか発生しない場合があります。

例えば:

ボタンの背景 = 黒 --> PRESS--> ボタンの背景 = 青

しかし、ボタンをもう一度押すと、Button BackGround が黒に戻ります (ToggleButton であるため)。

背景が 1 回だけ変化するようにするにはどうすればよいですか?

編集:ユーザーはボタンを押すときに理由を入力するため、ボタンは有効なままにしておく必要があります。つまり、間違った理由を選択した場合、それを変更できるということです。

<Style x:Key="ButtonStyleReg" TargetType="{x:Type myClasses:RegButton}">
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type ToggleButton}">
            <Grid x:Name="registrationButton">
                <Rectangle Name="rectangleBtn" Fill="#FF89959A" Height="Auto" RadiusY="15" RadiusX="15" Stroke="White" Width="Auto"/>

                <TextBlock x:Name="reason" TextWrapping="Wrap" 
                           Text="{Binding Reason, StringFormat=\{0\}}"
                           HorizontalAlignment="Center" Margin="0,7.5,0,0" Height="Auto" 
                           VerticalAlignment="Top" FontWeight="Bold" >
                </TextBlock>                                                                                   
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsFocused" Value="True"/>
                <!--<Trigger Property="IsDefaulted" Value="True"/>-->

                <Trigger Property="IsChecked" Value="True">
                    <Setter TargetName="rectangleBtn" Property="Fill" Value="blue" />
                </Trigger>                                                     
                <Trigger Property="IsEnabled" Value="False"/>
            </ControlTemplate.Triggers>
        </ControlTemplate>

    </Setter.Value>
</Setter>
<Setter Property="FontSize" Value="10.667"/>

スタイルを実装するリストボックス:

<ListBox x:Name="lbRegistration" ItemsSource="{Binding RegBtns, ElementName=Window}" Background="{x:Null}" 
            BorderBrush="{x:Null}" Grid.Column="1" 
            ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Disabled" Height="75">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <myClasses:RegistrationButton x:Name="RegistrationButton" HorizontalAlignment="Center" Height="71" Width="148" 
                                                  Margin="10,0,5,0" 
                                                  Style="{DynamicResource ButtonStyleRegistration}"
                                                  Click="RegistrationButton_Click"
                                                  Title="{Binding Title}"
                                                  Oorzaak="{Binding Oorzaak}"     

                                                  DuurStilstand="{Binding DuurStilstand, 
                                                        Converter={StaticResource  DateTimeConverter}}"

                                                  BeginStilstand="{Binding BeginStilstand}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

よろしく、ピート

4

2 に答える 2

1

非常に簡単なのは、コードビハインドから長方形にアクセスできる場合です。

rectangleBtn.Fill = Brushes.Blue;

アクセスできない場合は、2つのスタイルを作成してください。1つは元のスタイルで、もう1つはユーザーがクリックしたときに適用されるブルースタイルです。

コードビハインドでは、イベントClick="RegistrationButton_Click"でスタイルをBlueStyleに設定します。

RegistrationButton.Style = this.FindResource("ButtonStyleRegistration") as Style;

常に青にしたいので、このコードで十分です。それは常にあなたのためにそれを青にします。初めて、そして他の時間。そうすれば、要件を達成でき、スタイルは1回だけ変更されます。ウィンドウが読み込まれると、元のスタイル(最初のスタイル)に読み込まれます。したがって、XAMLにスタイル「黒」を入れ、上記のようにコードビハインドに入れます。

次に、これを削除する必要があります。

<Trigger Property="IsChecked" Value="True"> 
    <Setter TargetName="rectangleBtn" Property="Fill" Value="blue" /> 
</Trigger>    

次にこれ:

<myClasses:RegistrationButton x:Name="RegistrationButton" HorizontalAlignment="Center" Height="71" Width="148"  
Margin="10,0,5,0"  
Style="{DynamicResource ButtonStyleRegistration}" 
Click="RegistrationButton_Click"

そうなるでしょう:

<myClasses:RegistrationButton x:Name="RegistrationButton" HorizontalAlignment="Center" Height="71" Width="148"  
Margin="10,0,5,0"  
Style="{DynamicResource ButtonStyleRegBlack}" 
Click="RegistrationButton_Click"

以上です。

于 2012-05-10T13:32:30.403 に答える
1

MVVMアプローチを使用している場合は、背景とClickコマンドをViewModelのメンバーにバインドすることをお勧めします。ボタンを最初にクリックすると、フラグが設定され、背景色が変更されます。次にボタンがクリックされたときにフラグが設定されている場合、コマンドは背景を変更せずに戻ります。

XAML:

<ToggleButton Background="{Binding MyBackground}" Command="{Binding OnClickCmd}"/> 

ViewModel

public class ViewModel : INotifyPropertyChanged
{
     public Brush MyBackground 
     { 
          get { return background_; } 
          set {
                background_ = value;
                PropertyChanged(this, new PropertyChangedEventArg("MyBackground");
          }
     }

     public ICommand OnClickCmd
     {
          get {
               return new DelegateCommand(()=>{ // DelegateCommand implements ICommand
                  if(!isBackgroundSet) {
                      background = Brushes.Red;
                      isBackgroundSet_ = true;    
                  }
               });
          }
     }

     private Brush background_;
     private bool isBackgroundSet_;
     private event PropertyChangedEventHandler PropertyChagned;
 }
于 2012-05-10T13:48:52.750 に答える