2016-08-16 4 views
0

ItemsControl DataTemplate의 일부로 동적으로 설정되는 RadioButton이 있습니다.속성에 따라 RadioButton 스타일 변경

<RadioButton GroupName="Ratings"> 
    <RadioButton.Content> 
    <StackPanel Orientation="Horizontal"> 
     <TextBlock Text="{Binding Score}" /> 
     <TextBlock Text=" - " /> 
     <TextBlock Text="{Binding Description}" /> 
    </StackPanel> 
    </RadioButton.Content> 
</RadioButton> 

나는 내가의 RadioButton의 Style= 속성을 설정 개별적 때 잘 작동이 개 미리 정의 된 스타일 (MyCheckedStyle1MyUncheckedStyle2)를 가지고,하지만 난 그것을 속성을의 IsChecked 것에 따라 스타일을 변경할 수있는 방법을 발견하지 않았습니다.

대부분의 방법은 내가 나에게 내가 코드 숨김하여이 작업을 수행 할 수

if IsChecked = true then 
    style = MyCheckedStyle1 
else if IsChecked = false then 
    style = MyUncheckedStyle1 

그래서 의사 (ContentTemplate 트리거 등)에 대한 Style object is not allowed to affect the Style property of the object to which it applies. 예외를 제공하려고 시도하지만 난 경우이를 피하기 위해 노력하고 있어요 가능하고 XAML에 논리를 넣습니다.

+1

라디오의 '부모'스타일을 적용한 다음 트리거로 IsChecked 속성으로 라디오 스타일을 변경합니다. 라디오 자체에 스타일을 적용하는 또 다른 방법 트리거를 사용하여 ControlTemplate을 IsChecked 속성으로 변경합니다. – codeDom

+0

스타일이 나를 정의하지 않아 변경할 수 없습니다. 첫 번째 옵션이 작동하는 것처럼 들리지만, 의미를 더 설명하는 답을 제공 할 수 있습니까? –

+0

Checked/Unchecked 이벤트에서 코드의 스타일을 변경할 수 없습니까? –

답변

3

라디오의 부모에게 스타일을 적용한 다음 트리거로 IsChecked 속성으로 라디오 스타일을 변경합니다.

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="101" Width="264"> 
    <Window.Resources> 

     <Style x:Key="MyCheckedStyle1" TargetType="RadioButton"> 
      <Setter Property="Background" Value="Red"/> 
     </Style> 
     <Style x:Key="MyCheckedStyle2" TargetType="RadioButton"> 
      <Setter Property="Background" Value="Blue"/> 
     </Style> 

     <Style x:Key="ParentStyle" TargetType="ContentControl"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate> 
         <RadioButton Name="RadioButton1" GroupName="Ratings" > 
          <RadioButton.Content> 
           <StackPanel Orientation="Horizontal"> 
            <TextBlock Text="{Binding Score}" /> 
            <TextBlock Text=" - " /> 
            <TextBlock Text="{Binding Description}" /> 
           </StackPanel> 
          </RadioButton.Content> 
         </RadioButton> 
         <ControlTemplate.Triggers> 
          <Trigger SourceName="RadioButton1" Property="IsChecked" Value="True"> 
           <Setter TargetName="RadioButton1" Property="Style" 
             Value="{StaticResource MyCheckedStyle1}"/> 
          </Trigger> 
          <Trigger SourceName="RadioButton1" Property="IsChecked" Value="False"> 
           <Setter TargetName="RadioButton1" Property="Style" 
             Value="{StaticResource MyCheckedStyle2}"/> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Window.Resources> 
    <Grid> 
     <ListBox> 
      <ContentControl Style="{StaticResource ParentStyle}"/> 
      <ContentControl Style="{StaticResource ParentStyle}"/> 
     </ListBox> 
    </Grid> 
</Window> 
관련 문제