Search code examples
wpfxamlcheckboxbindingischecked

wpf xaml checkbox template in style


first post so please go easy. I am trying to get the value of a checkbox from another page (this is part of a navigation form in WPF project), but it always resets the value to false as soon as the page is changed. How do I get the IsChecked value to bind properly? I am learning just starting to work with WPF so the template is very basic, if there is a better way to do this then I am open to it.

Also the code behind page is vb.net not c# if that is going to play a part.

the style is called as follows:

<CheckBox Content="Photo" x:Name="OT_Photo" Grid.Row="0" Grid.Column="0" Style="{StaticResource ElementCheckbox}" />

There are several hundred checkboxes across these pages so I would very much like to avoid variables for each one.

The style is in 'Application.xaml'

<Style TargetType="CheckBox" x:Key="ElementCheckbox">
        <Setter Property="IsThreeState" Value="True" />
        <Setter Property="IsChecked" Value="{Binding IsChecked ,Mode=TwoWay}"  /> 
        <Setter Property="Foreground" Value="Navy" />
        <Setter Property="FontSize" Value="14" />
        <Setter Property="Margin" Value="15,4,0,4" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="MaxWidth" Value="500" />
        <Setter Property="Template">
              <Setter.Value>
                <ControlTemplate TargetType="{x:Type CheckBox}">
                    <DockPanel DockPanel.Dock="Left">
                        <Border x:Name="Border" CornerRadius="0" VerticalAlignment="Top">
                            <Image Height="20" Width="20" Name="CheckMark" />
                        </Border>
                        <TextBlock x:Name="CheckText" Text="{TemplateBinding Content}" Margin="5,0,0,0" FontSize="{TemplateBinding FontSize}" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" />
                    </DockPanel>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="{x:Null}">
                            <Setter TargetName="CheckMark" Property="Source" Value="CheckBoxNull.png" />
                        </Trigger>
                        <Trigger Property="IsChecked" Value="false">
                            <Setter TargetName="CheckMark" Property="Source" Value="CheckBoxCross.png" />
                        </Trigger>
                        <Trigger Property="IsChecked" Value="true">
                            <Setter TargetName="CheckMark" Property="Source" Value="CheckBoxTick.png" />
                        </Trigger>
                         <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Effect" TargetName="Border">
                                <Setter.Value>
                                    <DropShadowEffect ShadowDepth="1" Color="Blue" Opacity="1" BlurRadius="2"/>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Foreground" Value="Blue" TargetName="CheckText" />
                        </Trigger>
                          </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Thanks in advance of any help!

I should add I have been stuck on this for over a day, I have tried template binding, binding paths, and an on click procedure based on the c# answers I have found already. always resulting in the checkbox being reset though.


Solution

  • Solved

    My problem was unrelated to the checkbox as it happened, the page was not named statically so the properties always served the default values. (rookie mistake - much shame)

    on the up side the code above works perfectly (in its limited way) if anyone wants to pinch it!

    VB.NET:

    Private Sub MainForm_Loaded(sender As Object, e As RoutedEventArgs) Handles MainForm.Loaded
        Me.NavigationService.Navigate(ContactDetailsPage)
    End Sub
    

    instead of XAML stating the source:

    <NavigationWindow x:Name="MainForm" x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="" Source="ContactDetails.xaml">