2

I encountered a situation in the application i was developing. I want to write a template for a checkbox where i should be able to disable only the tick mark box and keep the label enabled. Is this really possible. I came up with a template, but when i disable the checkbox both the label and the tick mark get disabled.

0

2 Answers 2

3

just have a look at this xaml

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"  >
<Window.Resources>
    <Style x:Key="CheckBoxFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border>
                        <Rectangle Margin="15,0,0,0" Stroke="#60000000" StrokeThickness="1" StrokeDashArray="1 2"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <!-- PressedBrush is used for Pressed in Button, Radio Button, CheckBox -->
    <LinearGradientBrush x:Key="PressedBrush" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#BBB" Offset="0.0"/>
        <GradientStop Color="#EEE" Offset="0.1"/>
        <GradientStop Color="#EEE" Offset="0.9"/>
        <GradientStop Color="#FFF" Offset="1.0"/>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="PressedBorderBrush" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#444" Offset="0.0"/>
        <GradientStop Color="#888" Offset="1.0"/>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="NormalBrush" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#EEE" Offset="0.0"/>
        <GradientStop Color="#CCC" Offset="1.0"/>
    </LinearGradientBrush>
    <SolidColorBrush x:Key="GlyphBrush" Color="#444"/>
    <LinearGradientBrush x:Key="NormalBorderBrush" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#CCC" Offset="0.0"/>
        <GradientStop Color="#444" Offset="1.0"/>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="MouseOverBrush" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#FFF" Offset="0.0"/>
        <GradientStop Color="#AAA" Offset="1.0"/>
    </LinearGradientBrush>
    <!-- Simple CheckBox -->
    <Style x:Key="CheckBoxStyle1" TargetType="{x:Type CheckBox}">
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="FocusVisualStyle" Value="{DynamicResource CheckBoxFocusVisual}"/>
        <Setter Property="Background" Value="{DynamicResource NormalBrush}"/>
        <Setter Property="BorderBrush" Value="{DynamicResource NormalBorderBrush}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type CheckBox}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                            <ColumnDefinition></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <!-- BulletDecorator is used to provide baseline alignment between the checkmark and the Content -->
                        <BulletDecorator  x:Name="test" Grid.Column="0">
                            <BulletDecorator.Bullet>
                                <Grid Width="13" Height="13">
                                    <Border x:Name="Border" 
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}" 
                                BorderThickness="{TemplateBinding BorderThickness}"/>
                                    <Path x:Name="CheckMark" Stroke="{DynamicResource GlyphBrush}" 
                                StrokeThickness="2" 
                                SnapsToDevicePixels="False" Data="M 0 0 L 13 13 M 0 13 L 13 0"/>
                                </Grid>
                            </BulletDecorator.Bullet>
                        </BulletDecorator>
                        <ContentPresenter Grid.Column="1" 
                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                    Margin="{TemplateBinding Padding}" 
                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                    RecognizesAccessKey="True"/>

                    </Grid>
                    <!-- This uses Visibility to hide and show the CheckMark on IsChecked -->
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="false">
                            <Setter Property="Visibility" Value="Collapsed" TargetName="CheckMark"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Background" Value="{DynamicResource MouseOverBrush}" TargetName="Border"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter Property="Background" Value="{DynamicResource PressedBrush}" TargetName="Border"/>
                            <Setter Property="BorderBrush" Value="{DynamicResource PressedBorderBrush}" TargetName="Border"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Background" Value="gray" TargetName="Border"/>
                            <Setter Property="BorderBrush" Value="black" TargetName="Border"/>
                        </Trigger>

                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</Window.Resources>

<Grid x:Name="LayoutRoot">
    <CheckBox HorizontalAlignment="Left" 
    Margin="0,5,0,0" Style="{DynamicResource CheckBoxStyle1}" 
    VerticalAlignment="Top" Width="182" Height="17" 
    Content="Disabled" IsEnabled="False" IsChecked="True"  />
    <CheckBox HorizontalAlignment="Left" 
    Margin="125,5,0,0" Style="{DynamicResource CheckBoxStyle1}" 
    VerticalAlignment="Top" Width="182" Height="17" 
    Content="Active" IsEnabled="true" />
</Grid>

Sign up to request clarification or add additional context in comments.

1 Comment

This works exactly the way i wanted... Thanks a lot Kishore...:)@Kishore
1

I had the same problem and came up with a quite stupid solution:

<StackPanel  Orientation="Vertical"  >
        <TextBlock Text="Set Global Edition" TextWrapping="Wrap" HorizontalAlignment="Center" />
        <CheckBox x:Name="GlobalEdition" IsChecked="{Binding IsGlobal}" HorizontalAlignment="Center" />
</StackPanel>

Since the TextBlock & Checkbox are separated, changing the Checkbox state won't influence the text attached to it.

(If I remember well, a Textbox does not change its appearance if disabled, maybe you could try with a TextBox instead of a label)

6 Comments

Well, i tried with a textbox and no luck with that also. I am actually writing a template for it and the whole thing gets disabled when the disable the checkbox.@Damascus
Do you put the textbox in the Checkbox? Because that might be the problem, if it is insite the Checkbox it will undeniably be linked to the box's state (this is actually why I put two separate controls in a StackPanel rather than adding a textBlock in the checkBox which, as you just saw, doesn't work that way)
In the template i put both the Textbox and checkbox again. @Damascus
Well, if you declare a content presenter instead of textBox and bind the content property with the Content property of checkbox you can accomplish the task. The answer provided above will help you know more about this. Thank you all for replying @Damascus, @Kishore Kumar
The code above just gave me a hint for finding out the solution to my problem. The solution is simple only. Just replace the textbox in the template with a contentPresenter. I think this solution is very simple.:)@Damascus
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.