Skip to main content

Microsoft Silverlight

Answered Question accessing checkbox template items in code behindRSS Feed

(0)

pdjplano
pdjplano

Member

Member

59 points

38 Posts

accessing checkbox template items in code behind

Hi, I have a sample that is pretty simple, a checkbox control is customized so that it has an image and an associated border around the image when it is checked.  I have the following defined under the <UserControl.Resources> of my Page.xaml and want to, in the code behind, get access to the image defined as x:Name="cbImage" therein. 

First, the style:

   <vsm:Style x:Key="CruiseLineCheckBoxStyle" TargetType="CheckBox">
   <vsm:Setter Property="IsEnabled" Value="true"/>
   <vsm:Setter Property="IsTabStop" Value="true"/>
   <vsm:Setter Property="Background" Value="#FFFFFFFF"/>
   <vsm:Setter Property="Foreground" Value="#00000000"/>
   <vsm:Setter Property="Margin" Value="0"/>
   <vsm:Setter Property="HorizontalContentAlignment" Value="Center"/>
   <vsm:Setter Property="VerticalContentAlignment" Value="Center"/>
   <vsm:Setter Property="Cursor" Value="Arrow"/>
   <vsm:Setter Property="TextAlignment" Value="Center"/>
   <vsm:Setter Property="TextWrapping" Value="NoWrap"/>
   <vsm:Setter Property="FontSize" Value="9"/>
   <vsm:Setter Property="Template">
    <vsm:Setter.Value>
     <ControlTemplate TargetType="CheckBox">
      <Grid Width="140" Height="64">
       <vsm:VisualStateManager.VisualStateGroups>
        <vsm:VisualStateGroup x:Name="CheckStates">
         <vsm:VisualState x:Name="Checked">
          <Storyboard>
           <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
            <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF0000FF"/>
           </ColorAnimationUsingKeyFrames>
          </Storyboard>
         </vsm:VisualState>
         <vsm:VisualState x:Name="Unchecked">
          <Storyboard>
           <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.Opacity)">
            <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
           </DoubleAnimationUsingKeyFrames>
          </Storyboard>
         </vsm:VisualState>
        </vsm:VisualStateGroup>
       </vsm:VisualStateManager.VisualStateGroups>
       <Rectangle Margin="2,2,2,2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" RadiusX="3" RadiusY="3" StrokeThickness="1" Fill="#FFECD6EA" x:Name="rectangle"/>
       <Rectangle Margin="4,4,4,4" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" RadiusX="3" RadiusY="3" StrokeThickness="1" Fill="#FFFFFFFF" x:Name="rectangleInner"/>
       <Image Margin="4,4,4,4" x:Name="cbImage" Source="carnival_logo.jpg" Stretch="Uniform"/>
      </Grid>
     </ControlTemplate>
    </vsm:Setter.Value>
   </vsm:Setter>
  </vsm:Style>

 ok, so once the style is defined i create a control with that style:

<CheckBox x:Name="CarnivalCB" Style="{StaticResource CruiseLineCheckBoxStyle}" IsChecked="False" />

and now, in code, I want to access the CheckBox Image so I can set the Source {we are using this same control for multiple images and so need to programatically change each image based on a database access query for each Cruise line}

Thanks in advance, I just can't see how to access the underlying image object!!!

Dave Relyea
Dave Relyea

Participant

Participant

1084 points

249 Posts

Microsoft

Re: accessing checkbox template items in code behind

FIrst of all, you don't need the vsm: prefix on the Style, Setter, or Setter.Value elements.

Second, since you can't databind the image source property (nothing to bind to on CheckBox) you'll have to subclass CheckBox. Then you can either databind to a property that you add to the CheckBox, or find the Image in OnApplyTemplate.

pdjplano
pdjplano

Member

Member

59 points

38 Posts

Re: accessing checkbox template items in code behind

Thanks, the vsm: was added by Blend, do I just remove it?

 On the second part, that's what i've come to realize the more I've read, time to subclass CheckBox and do my own thing in it.  More reading!!

 

Thanks,

Paul

 

pdjplano
pdjplano

Member

Member

59 points

38 Posts

Re: accessing checkbox template items in code behind

after removing the vsm: prefix it no longer works right in blend, what's missing?

 also, is there a good link to a "creating a custom silverlight control in beta 2" somewhere that shows how to derive from a built-in control and change?

TIA,
Paul

 

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Answered Question

Re: accessing checkbox template items in code behind

Hello, try this:

public class ImageCheckBox : CheckBox
{
public ImageCheckBox()
{
this.DefaultStyleKey = typeof(ImageCheckBox);
}

public static readonly DependencyProperty CheckedImageProperty = DependencyProperty.Register("CheckedImage", typeof(ImageSource), typeof(ImageCheckBox), new PropertyMetadata(null));

public ImageSource CheckedImage
{
get { return (ImageSource)GetValue(CheckedImageProperty); }
set { SetValue(CheckedImageProperty, value); }
}
}

 

Generic.xaml:

<Style TargetType="local:ImageCheckBox">
<Setter Property="IsEnabled" Value="true"/>
<Setter Property="IsTabStop" Value="true"/>
<Setter Property="Background" Value="#FF003255"/>
<Setter Property="Foreground" Value="#FF313131"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Cursor" Value="Arrow"/>
<Setter Property="TextAlignment" Value="Left"/>
<Setter Property="TextWrapping" Value="NoWrap"/>
<Setter Property="FontSize" Value="11"/>

<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:ImageCheckBox">
<StackPanel Orientation="Horizontal" Background="Transparent">
<vsm:VisualStateManager.VisualStateGroups>
<vsm:VisualStateGroup x:Name="CheckStates">
<vsm:VisualStateGroup.Transitions>
<vsm:VisualTransition Duration="00:00:00.2000000"/>
</vsm:VisualStateGroup.Transitions>
<vsm:VisualState x:Name="Checked">
<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="image" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</vsm:VisualState>
<vsm:VisualState x:Name="Unchecked"/>
</vsm:VisualStateGroup>
</vsm:VisualStateManager.VisualStateGroups>

<Grid>
<Rectangle HorizontalAlignment="Left" Width="20" Stroke="#FF00CCFF" RadiusX="2" RadiusY="2" Height="20"/>
<Image Width="15" Height="15" Source="{TemplateBinding CheckedImage}" Opacity="0" x:Name="image"/>
</Grid>
<ContentPresenter VerticalAlignment="Center" Margin="10,0,0,0"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

 

To use:

<local:ImageCheckBox CheckedImage="an image.jpg" Content="ImageCheckBox"/>

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

pdjplano
pdjplano

Member

Member

59 points

38 Posts

Re: Re: accessing checkbox template items in code behind

i created a new project with just your code in it to try to get this to work and it compiles fine then at runtime i get an error of : AG_E_UNKNOWN_ERROR [Line: 9 Position: 83] in the Page.xaml that is trying to include the ImageCheckbox.

Isn't there a GOOD FULL tutorial somewhere of creating derivative controls from Silverlight base classes?  I keep finding snippet after snippet that doesn't fully work and it's most annoying.

 

lee_sl
lee_sl

Contributor

Contributor

2992 points

585 Posts

Re: Re: Re: accessing checkbox template items in code behind

it seem to work fine

----------------------------------------------
Available for consulting in Dallas, TX
http://leeontech.wordpress.com/

pdjplano
pdjplano

Member

Member

59 points

38 Posts

Re: Re: Re: accessing checkbox template items in code behind

would you mind sending me a .zip of the small project that this is working in?

lee_sl
lee_sl

Contributor

Contributor

2992 points

585 Posts

Answered Question

Re: Re: Re: accessing checkbox template items in code behind

sent an email with .zip

----------------------------------------------
Available for consulting in Dallas, TX
http://leeontech.wordpress.com/

pdjplano
pdjplano

Member

Member

59 points

38 Posts

Re: Re: Re: Re: accessing checkbox template items in code behind

ah, thanks!  missed the element assignment for the image, so nothing was ever showing up.  now i can do it, thanks lee__!

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities