2017-02-07 1 views
1

그룹화 된 라디오 버튼을 토글 버튼으로 스타일을 지정해야합니다. 이렇게하려면 라디오 버튼에 다음 스타일을 적용했습니다.스타일 WPF 라디오 버튼을 올바른 IsEnabled 동작으로 토글 버튼으로 사용

Style="{StaticResource {x:Type ToggleButton}}" 

이것은 내가 원하는 스타일을 제공하지만 성가신 부작용을 발견했습니다. 컨트롤을 사용할 수없는 상태에서 선택한 버튼을 변경할 수 있어야합니다. 정상적인 라디오 단추로 예상대로 작동합니다. 그러나 토글 단추 스타일 단추를 사용하면 더 이상 단추 중 하나가 선택되지 않습니다.

다음의 데모에서 '켜기/끄기'버튼을 반복해서 클릭하면 다시 활성화 할 때 선택한 버튼이 계속 강조 표시됩니다. 그러나 비활성화 된 상태에서 선택한 버튼을 변경 한 다음 다시 활성화하면 ('토글 활성화 됨', '값 변경', '토글 활성화 됨') 아무 버튼도 강조 표시되지 않습니다.

것은 내가 달성하기 위해 노력하고있어 :

  1. 이 ToggleButton을의 스타일에 최대한 가깝게 유지하십시오.
  2. 체크 된 버튼을 사용하지 않으면 파란색 배경을 유지하지만 불투명도를 유지합니다.
  3. 값이 변경되었는지 여부에 관계없이 단추를 다시 사용할 때 표준 ToggleButton 스타일을 사용하십시오.

XAML : 뒤에

<Window x:Class="ToggleButtonDemo.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:ToggleButtonDemo" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="200" Width="200" 
     Name="demoWindow" 
     DataContext="{Binding ElementName=demoWindow}"> 
    <StackPanel> 
     <GroupBox Header="Radio" IsEnabled="{Binding Enable}"> 
      <StackPanel Orientation="Horizontal"> 
       <RadioButton Name="radio1" Content="One" GroupName="RadioGroup" IsChecked="True"/> 
       <RadioButton Name="radio2" Content="Two" GroupName="RadioGroup"/> 
      </StackPanel> 
     </GroupBox> 
     <GroupBox Header="Toggle" IsEnabled="{Binding Enable}"> 
      <StackPanel Orientation="Horizontal"> 
       <RadioButton Name="toggle1" Content="One" GroupName="ToggleGroup" Style="{StaticResource {x:Type ToggleButton}}" IsChecked="True"/> 
       <RadioButton Name="toggle2" Content="Two" GroupName="ToggleGroup" Style="{StaticResource {x:Type ToggleButton}}"/> 
      </StackPanel> 
     </GroupBox> 
     <Button Name="toggle" Content="Toggle enabled" Click="toggle_Click"/> 
     <Button Name="changeValue" Content="Change value" Click="changeValue_Click"/> 
    </StackPanel> 
</Window> 

코드 :

using System.ComponentModel; 
using System.Windows; 

namespace ToggleButtonDemo 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window, INotifyPropertyChanged 
    { 

     private bool mEnable = true; 

     public bool Enable 
     { 
      get 
      { 
       return mEnable; 
      } 
      set 
      { 
       mEnable = value; 
       OnPropertyChanged(nameof(Enable)); 
      } 
     } 

     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     public virtual void OnPropertyChanged(string propertyName) 
     { 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
     } 

     private void toggle_Click(object sender, RoutedEventArgs e) 
     { 
      Enable = !Enable; 
     } 

     private void changeValue_Click(object sender, RoutedEventArgs e) 
     { 
      if (radio1.IsChecked == true) 
      { 
       radio2.IsChecked = true; 
      } 
      else if (radio2.IsChecked == true) 
      { 
       radio1.IsChecked = true; 
      } 

      if (toggle1.IsChecked == true) 
      { 
       toggle2.IsChecked = true; 
      } 
      else if (toggle2.IsChecked == true) 
      { 
       toggle1.IsChecked = true; 
      } 
     } 
    } 
} 

답변

2

이 비활성화 된 ToggleButton가 보이는 방법이다. 모양을 변경하려면 사용자 정의 ControlTemplate을 정의해야합니다. 다음 예를 참조하십시오 :

<Window x:Class="ToggleButtonDemo.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:ToggleButtonDemo" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="200" Width="200" 
    Name="demoWindow" 
    DataContext="{Binding ElementName=demoWindow}"> 
<Window.Resources> 
    <Style x:Key="FocusVisual"> 
     <Setter Property="Control.Template"> 
      <Setter.Value> 
       <ControlTemplate> 
        <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
    <SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD"/> 
    <SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070"/> 
    <SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD"/> 
    <SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1"/> 
    <SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6"/> 
    <SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B"/> 
    <SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4"/> 
    <SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5"/> 
    <SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383"/> 
    <Style TargetType="{x:Type ToggleButton}"> 
     <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/> 
     <Setter Property="Background" Value="{StaticResource Button.Static.Background}"/> 
     <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/> 
     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="HorizontalContentAlignment" Value="Center"/> 
     <Setter Property="VerticalContentAlignment" Value="Center"/> 
     <Setter Property="Padding" Value="1"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ToggleButton}"> 
        <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> 
         <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="Button.IsDefaulted" Value="true"> 
          <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> 
         </Trigger> 
         <Trigger Property="IsMouseOver" Value="true"> 
          <Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/> 
          <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/> 
         </Trigger> 
         <Trigger Property="IsPressed" Value="true"> 
          <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/> 
          <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/> 
         </Trigger> 
         <Trigger Property="IsChecked" Value="True"> 
          <Setter Property="Background" TargetName="border" Value="#FFBCDDEE"/> 
          <Setter Property="BorderBrush" TargetName="border" Value="#FF245A83"/> 
         </Trigger> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Opacity" TargetName="border" Value="0.7"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 
<StackPanel> 
    <GroupBox Header="Radio" IsEnabled="{Binding Enable}"> 
     <StackPanel Orientation="Horizontal"> 
      <RadioButton Name="radio1" Content="One" GroupName="RadioGroup" IsChecked="True"/> 
      <RadioButton Name="radio2" Content="Two" GroupName="RadioGroup"/> 
     </StackPanel> 
    </GroupBox> 
    <GroupBox Header="Toggle" IsEnabled="{Binding Enable}"> 
     <StackPanel Orientation="Horizontal"> 
      <RadioButton Name="toggle1" Content="One" GroupName="ToggleGroup" Style="{StaticResource {x:Type ToggleButton}}" IsChecked="True"/> 
      <RadioButton Name="toggle2" Content="Two" GroupName="ToggleGroup" Style="{StaticResource {x:Type ToggleButton}}"/> 
     </StackPanel> 
    </GroupBox> 
    <Button Name="toggle" Content="Toggle enabled" Click="toggle_Click"/> 
    <Button Name="changeValue" Content="Change value" Click="changeValue_Click"/> 
</StackPanel> 
</Window> 

enter image description here

+0

이 중대하다. 그것은 나에게 내가 바라는 행동을 준다. 너무 많은 스타일을 재정의하지 않고도이 작업을 수행 할 수 있습니까? 표준 ToggleButton 스타일을 더 많이 보유하고 싶습니다. – sclarke81

+0

아니요, 전체 템플릿을 다시 정의해야합니다. 템플릿의 일부만 재정의 할 수 없습니다. 그러나 이것은 약간의 수정을 가한 Windows 10에서의 기본 템플릿 모양과 같습니다. VS 또는 Blend의 디자인 모드에서 ToggleButton을 마우스 오른쪽 단추로 클릭하고 템플릿 편집 -> 복사본 편집을 선택하고 요구 사항에 따라 XAML 태그에 기본값을 복사 할 수 있습니다. – mm8

+0

고마워,이게 내가 필요로하는 것을 얻었다. – sclarke81

관련 문제