2014-03-06 6 views
0

저는 WPF를 처음 사용하고 있으며 여기에 내가하려고하는 것이 있습니다. 라디오 버튼이 선택되어 있지 않은지 여부에 따라 정적 리소스 인 버튼의 스타일을 변경하고 싶습니다. 그래서 '예'를 선택하고, 내가 BlueButton (다른 정적 자원)과의 '진행'버튼 스타일을 변경하고자 할 때WPF MVVM 정적 리소스 스타일을 동적으로 변경합니다.

<RadioButton Name="rbInfoCorrect" 
      GroupName="InfoQuestion" 
      cal:Message.Attach="ShouldProceed"> 
     <RadioButton.Content> 
     <TextBlock Text="Yes" Foreground="White"/> 
     </RadioButton.Content> 
</RadioButton> 
<RadioButton Name="rbInfoNotCorrect" 
      GroupName="InfoQuestion" 
      cal:Message.Attach="ShouldStop"> 
     <RadioButton.Content> 
     <TextBlock Text="No" Foreground="White"/> 
     </RadioButton.Content> 
</RadioButton> 


<Button x:Name="btnProceed" cal:Message.Attach="Proceed"> 
    <Button.Style> 
    <Style TargetType="Button" BasedOn="{StaticResource WhiteButton}"/> 
    </Button.Style> 
</Button> 
<Button x:Name="btnStop" cal:Message.Attach="Stop"> 
    <Button.Style> 
    <Style TargetType="Button" BasedOn="{StaticResource WhiteButton}"/> 
    </Button.Style> 
</Button> 

'아니오'를 선택하고 난을 변경하려면 : 다음 샘플 코드는 BlueButton에 '중지'버튼 스타일. 물론 이것이 더 큰 문제의 일부이기 때문에 샘플 예제를 구체적으로 다루려고합니다.

내가 꼼짝 못하고있는 부분은 라디오 버튼 상태 확인에 따라 StaticResource를 WhiteButton에서 BlueButton으로 업데이트하는 방법입니다. 어떤 방향이라면 크게 감사하겠습니다.

감사합니다.

답변

2

우리는 IMultiValue 변환기를 사용하여 전체 스타일을 바꿀 수 있습니다. 귀하의 요구에 대한

나는 내 문제를 해결하기 위해이 좋은 기사 http://social.msdn.microsoft.com/Forums/vstudio/en-US/b25973bb-9e9c-4a99-8234-39a042e0a478/apply-styles-dynamically-to-buttons-in-xaml?forum=wpf 이전 버전을 사용하고

<Window x:Class="StackWpf.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:StackWpf" 
     Title="MainWindow" Name="window" Height="350" Width="525" > 
    <Window.Resources> 

     <ResourceDictionary> 
      <Style TargetType="Button" x:Key="WhiteStyle"> 
       <Setter Property="Background" Value="White"/> 
      </Style> 
      <Style TargetType="Button" x:Key="BlueStyle"> 
       <Setter Property="Background" Value="Blue"/> 
      </Style> 
      <local:StyleConverter x:Key="styleConverter"/> 
     </ResourceDictionary> 
    </Window.Resources> 

    <Grid> 
     <RadioButton Name="rbInfoCorrect" IsChecked="False" 
      GroupName="InfoQuestion" Margin="80,19,382,257"> 
      <RadioButton.Content> 
       <TextBlock Text="Yes" Foreground="Black"/> 
      </RadioButton.Content> 
     </RadioButton> 
     <RadioButton Name="rbInfoNotCorrect" IsChecked="False" 
      GroupName="InfoQuestion" Margin="80,38,391,257"> 
      <RadioButton.Content> 
       <TextBlock Text="No" Foreground="Black"/> 
      </RadioButton.Content> 
     </RadioButton> 
     <Button Content="Button" Margin="80,114,294,161"> 
      <Button.Style> 


         <MultiBinding Converter="{StaticResource styleConverter}"> 
          <Binding ElementName="rbInfoCorrect" 
          Path="IsChecked" /> 
          <Binding ElementName="rbInfoNotCorrect" 
          Path="IsChecked" /> 
          <Binding Source="{StaticResource WhiteStyle}" /> 
          <Binding Source="{StaticResource BlueStyle}" /> 
         </MultiBinding> 


      </Button.Style> 
     </Button> 

    </Grid> 
</Window> 

이 예

public class StyleConverter : IMultiValueConverter 
    { 
     public object Convert(object[] values, Type targetType, 
     object parameter, CultureInfo culture) 
     { 
      if (values.Length < 1) 
       return Binding.DoNothing; 
      bool isCorrect = (bool)values[0]; 
      bool isNotCorrect = (bool)values[1]; 
      Style firstStyle = (Style)values[2]; 
      Style secondStyle = (Style)values[3]; 
      if (isCorrect) 
       return firstStyle; 
      if (isNotCorrect) 
       return secondStyle; 
      return Binding.DoNothing; 

     } 

     public object[] ConvertBack(object value, Type[] targetTypes, 
      object parameter, CultureInfo culture) 
     { 
      throw new NotSupportedException(); 
     } 
    } 

XAML 함께했다.

1

난 당신이 전체 스타일을 대체 할 수 있는지 확실하지 않습니다하지만 당신은 유저 (A) DataTrigger 버튼의 일부 속성을 변경할 수 있습니다

<Button.Style> 
     <Style TargetType="Button" BasedOn="{StaticResource WhiteButton}"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding ElementName=rbInfoCorrect, Path=IsChecked}" Value="True"> 
      <Setter Property="Background" Value="White" /> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding ElementName=rbInfoCorrect, Path=IsChecked}" Value="False"> 
      <Setter Property="Background" Value="Blue" /> 
      </DataTrigger> 
     </Style.Triggers> 
     </Style> 
    </Button.Style> 
+0

답장을 보내 주셔서 감사합니다.하지만 그라디언트 및 다른 것들과 같은 배경 속성을 변경하는 방법을 찾고 있었기 때문에 기존 스타일 리소스를 대신 재사용 할 수있는 방법을 원했습니다. – Harish

1

Style을 변경하지 않아도됩니다. 간단하게 DataTrigger을 추가하면 Checkbox.IsChecked 속성에 직접 반응 할 수 있습니다. 그냥 DataTrigger 내부의 Checkbox.IsChecked 속성에 의해 유발 일반 Setter의 같은 기본 사람과 사람을 선언 할 수 있습니다,

<Style x:Key="ProceedButtonStyle" TargetType="{x:Type Button}"> 
    <Setter Property="Background" Value="White" /> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding IsChecked, ElementName=rbInfoCorrect}" 
      Value="True"> 
      <Setter Property="Background" Value="Blue" /> 
     </DataTrigger> 
    </Style.Triggers> 
</Style> 

<Style x:Key="StopButtonStyle" TargetType="{x:Type Button}"> 
    <Setter Property="Background" Value="White" /> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding IsChecked, ElementName=rbInfoNotCorrect}" 
      Value="True"> 
      <Setter Property="Background" Value="Blue" /> 
     </DataTrigger> 
    </Style.Triggers> 
</Style> 

을 더 많은 속성을 변경하려는 경우에도 : 그것은이 대신 같은 Style의 선언까지 간단 . 두 개의 Style에 공통적 인 속성이 많이 있다면, 그것들을 모두 하나로 선언하고 그 중 하나를 기반으로 다른 것을 추가 할 수 있습니다. 다른 DataTrigger을 추가하십시오.

+0

답장을 보내 주셔서 감사합니다. 그러나 트리거에 설정할 속성이 몇 개 이상 있으므로 전체 스타일을 대체하려고했습니다. 그래서, 속성을 설정하는 대신 기존 스타일 리소스를 어떤 식 으로든 재사용하려고했습니다. – Harish

0

체크 아웃 http://Globalizer.codeplex.com을 확인하십시오.

언어를 전환 할 때 전체 스타일을 전환합니다.

기본적으로 리소스 사전에서 스타일을 찾아서 죽이고 새 리소스를로드합니다.

관련 문제