2016-05-14 17 views
0

私は3つのラジオボタン「はい/いいえ/保留中」を含む灰色のグリッドを持っていますが、選択した値に基づいてグリッドの色を変更します。RadioButtonに基づいてグリッドの背景色を変更します

値がyesの場合、グリッドは緑色になります。

valieがNoの場合はグリッドが赤くなります。

<Grid Background="#FFF2F2F2" Height="34" HorizontalAlignment="Left" Name="grid19" VerticalAlignment="Top" Width="151"> 
         <RadioButton Content="Yes" Height="16" HorizontalAlignment="Left" Margin="2,12,0,0" Name="radioButton1" VerticalAlignment="Top" /> 
         <RadioButton Content="No" Height="16" HorizontalAlignment="Left" Margin="46,12,0,0" Name="radioButton2" VerticalAlignment="Top" /> 
         <RadioButton Content="Pending" Height="16" HorizontalAlignment="Left" Margin="86,12,0,0" Name="radioButton3" VerticalAlignment="Top" /> 
        </Grid> 

多くの感謝^^私はデフォルトを削除した

答えて

0
<Grid Height="60" HorizontalAlignment="Left" Name="grid19" VerticalAlignment="Top" Width="151"> 
     <RadioButton Content="Yes" Height="16" HorizontalAlignment="Left" Margin="2,12,0,0" Name="radioButton1" VerticalAlignment="Top" /> 
     <RadioButton Content="No" Height="16" HorizontalAlignment="Left" Margin="46,12,0,0" Name="radioButton2" VerticalAlignment="Top" /> 
     <RadioButton Content="Pending" Height="16" HorizontalAlignment="Left" Margin="86,12,0,0" Name="radioButton3" VerticalAlignment="Top" /> 
     <Grid.Style> 
      <Style TargetType="Grid"> 
       <Setter Property="Background" Value="#FFF2F2F2" /> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding ElementName=radioButton1, Path=IsChecked}" Value="True"> 
         <Setter Property="Background" Value="Red" /> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding ElementName=radioButton2, Path=IsChecked}" Value="True"> 
         <Setter Property="Background" Value="Blue" /> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding ElementName=radioButton3, Path=IsChecked}" Value="True"> 
         <Setter Property="Background" Value="Green" /> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </Grid.Style> 
    </Grid> 

注:

と私はすべてのことしかありませんコードの後ろにXAMLで発生する場合は、 はここに私のコードですバックグラウンドを追加してセッターとして追加します。これを直接追加すると、他のバックグラウンドプロパティがすべてオーバーライドされるためです。これがあなたを助けることを願っています。

+1

thanlsさんが働いています^^ –

関連する問題