2012-06-13 3 views
4

체크 박스 (C# 코드에서)을 체크하지 못하게하고 싶습니다. 선택을 취소 할 수 있습니다.
어떻게해야합니까?WPF에서 체크 박스 검사 사용 안 함

ps.
이벤트 핸들러를 에 작성하십시오. 이벤트가을 클릭하면 수락되지 않는 것을 확인한 후 즉시 선택을 취소합니다.

[편집]
사용자가 확인란을 선택할 수 있다고 생각할 수 있도록 확인란이 항상 활성화되어 있어야합니다. 그것은 이상하지만 특정 프로그램입니다.

+2

사용자가 상자의 선택을 취소하면 다시 확인할 수 있습니까? – Dylan

답변

9
<CheckBox Content="Checkbox" 
      IsEnabled="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}" /> 

편집 후에 체크는 항상 그렇지 않은 경우에도 때, 체크 가능한 상태에있는 것으로보고있다한다는 말. 다음은 작동 예제입니다.

<Window 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" x:Class="WpfTest.MainWindow" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <ControlTemplate x:Key="DeceptiveCheckbox" TargetType="{x:Type CheckBox}"> 
      <BulletDecorator Background="Transparent" SnapsToDevicePixels="True"> 
       <BulletDecorator.Bullet> 
        <Microsoft_Windows_Themes:BulletChrome BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" IsChecked="{TemplateBinding IsChecked}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}"/> 
       </BulletDecorator.Bullet> 
       <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
      </BulletDecorator> 
      <ControlTemplate.Triggers> 
       <Trigger Property="HasContent" Value="True"> 
        <Setter Property="FocusVisualStyle"> 
         <Setter.Value> 
          <Style> 
           <Setter Property="Control.Template"> 
            <Setter.Value> 
             <ControlTemplate> 
              <Rectangle Margin="14,0,0,0" SnapsToDevicePixels="True" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/> 
             </ControlTemplate> 
            </Setter.Value> 
           </Setter> 
          </Style> 
         </Setter.Value> 
        </Setter> 
        <Setter Property="Padding" Value="4,0,0,0"/> 
       </Trigger> 
      </ControlTemplate.Triggers> 
     </ControlTemplate> 
    </Window.Resources> 
    <Grid> 
     <CheckBox Content="CheckBox" IsEnabled="{Binding IsChecked, RelativeSource={RelativeSource Self}}" Template="{StaticResource DeceptiveCheckbox}" /> 
    </Grid> 
</Window> 

수정 사항은 간단합니다. 나는 단순히 현재의 체크 박스 템플릿의 복사본을 생성 및 제거 :

<Trigger Property="IsEnabled" Value="False"> 
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
</Trigger> 
2

쉬운 해결책은 IsEnabled 속성을 IsChecked 속성과 동일하게 바인딩하는 것입니다.

+0

체크 상자를 더 이상 사용할 수 없다는 사실을 시각적으로 알려주기 때문에 좋은 해결책입니다. (IsReaded는 IsEnabled 여야하고 반대가 아닌 것을 제외하고는 IsReadOnly는 TextBox 전용입니다.) –

+0

IsReadOnly가 wpf 확인란의 유효한 속성이 아닙니다. –

+0

좋은 점, 편집 –

2

어떻게 이것에 대해 :

<Grid> 
    <Grid.Resources> 
     <Style TargetType="CheckBox"> 
      <Style.Triggers> 
       <Trigger Property="IsChecked" Value="False"> 
        <Setter Property="IsEnabled" Value="False" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Grid.Resources> 

    <CheckBox IsChecked="True" /> 
</Grid> 

이 방식이있다 (있는 그대로) 전부 또는 대부분에 적용되는 이익 (있는 경우 x:Key 속성을 적용한 다음 특별한 체크를하지 않거나 개별 체크 박스의 바인딩을 변경할 필요없이 원하는 체크 박스에 리소스를 할당하십시오.

+0

비활성화 된 체크 박스의 색상을 변경할 수 있습니까? 체크 상자의 사각형의 색을 바꿀 수 있다면 솔루션이 좋을 것입니다. Checkable이 여전히 'checkable'인 것처럼 보이게해야합니다. –

+0

아니요, 자체 컨트롤 템플릿을 구현하지 않아도됩니다. 컨트롤을 비활성화하지 않으려면 확인란을 선택할 수 있는지 또는 확인할 수 없는지를 결정하는 코드 숨김 이벤트 처리기가 필요합니다. – CodingGorilla