Custom Control editing in code behind
Last post 05-12-2008 6:19 AM by valleyman86. 4 replies.
Sort Posts:
05-11-2008 9:48 PM
Custom Control editing in code behind

 I am creating a custom control and have a generic.xaml page. I am trying to get the size of the buttons/other controls in the custom control. I am using a itemscontrol. I can not seem to use these in the code behind at all. Below is some xaml in my generic.xaml file and the code I tried to use in the cs file. The InitializeFromXaml always fails because it says generic.xaml is an invalid xaml file (due to its root not being a FrameworkElement).

 

The other solution I have would be to change the size of the buttons in my HorizontalWrapPanel. I would do this except I can not figure out how to resize a child UIElement in the  panel class.

 

This does not work:

System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("ClassSchedulerControls.generic.xaml");
FrameworkElement implementationRoot = this.InitializeFromXaml(new System.IO.StreamReader(s).ReadToEnd());
HorizontalWrapPanel sb = (HorizontalWrapPanel)implementationRoot.FindName("WrapSectionItems");

This is the generic.xaml default style for my control.

<Style TargetType="controls:SectionSelectControl">
        <Setter Property="Width" Value="698" />
        <!--<Setter Property="Height" Value="198" />-->
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="controls:SectionSelectControl">
                    <Border BorderBrush="Black" BorderThickness="2" CornerRadius="5"  HorizontalAlignment="Center" VerticalAlignment="Center">
                        <ScrollViewer MinWidth="698" VerticalScrollBarVisibility="Auto" >
                            <ItemsControl x:Name="SectionItems" ItemsSource="{TemplateBinding SectionItemsSource}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <controls:HorizontalWrapPanel x:Name="WrapSectionItems" />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <ToggleButton Style="{StaticResource ToggleButtonStyle}" Height="{Binding Size}" Content="{Binding TestString1}"></ToggleButton>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>

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

valleyman86

Joined on 05-11-2008
Posts 3
05-12-2008 1:41 AM
Re: Custom Control editing in code behind

That's not the way to access the visual elements on the custom control from within the custom control code (are using SL 1.1 code?). You need to access the visaul elements in the OnApplyTemplate() method:

protected override void OnApplyTemplate()
{
    RootElement = (FrameworkElement)GetTemplateChild("RootElement");
    TextElement = (TextBlock)GetTemplateChild("TextElement");
    UpButtonElement = (RepeatButton)GetTemplateChild("UpButtonElement");
    DownButtonElement = (RepeatButton)GetTemplateChild("DownButtonElement");

}

There are a few things to keep in mind as far as creating a custom control is created. Please read this quickstart carefully.

Hope this helps,
Jim (http://jimmangaly.blogspot.com/)

Please MARK the replies as answers if they answered your question

http://www.identitymine.com/

Jim Mangaly

Joined on 04-21-2008
Kochi, India
Posts 108
05-12-2008 2:45 AM
Re: Re: Custom Control editing in code behind

Yes I did this and I was able to get access to the ItemsControl but this seems worthless as the itemshost is always null. It seems like this silverlight 2.0 beta is a series of hacks and workarounds because only half of anything seems to work properly.

 

valleyman86

Joined on 05-11-2008
Posts 3
05-12-2008 4:27 AM
Marked as Answer
Re: Re: Custom Control editing in code behind

I was able to get hold of the ItemsHost. I made some small modifications to your code, so that I could try it out easily. The ItemsHost of the ItemsControl was only available after the control was loaded: 

Generic.xaml

<Style TargetType="controls:SectionSelectControl">

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="controls:SectionSelectControl">

<Border x:Name="RootElement" BorderBrush="Black" BorderThickness="2" CornerRadius="5" HorizontalAlignment="Center" VerticalAlignment="Center">

<ScrollViewer MinWidth="698" VerticalScrollBarVisibility="Auto" >

<ItemsControl x:Name="SectionItems" >

<ItemsControl.ItemsPanel>

<ItemsPanelTemplate>

<StackPanel x:Name="WrapSectionItems" />

</ItemsPanelTemplate>

</ItemsControl.ItemsPanel>

<ItemsControl.ItemTemplate>

<DataTemplate>

<ToggleButton></ToggleButton>

</DataTemplate>

</ItemsControl.ItemTemplate>

</ItemsControl>

</ScrollViewer>

</Border>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

SelectionSelectControl.cs

protected override void OnApplyTemplate()

{

itemsControl = (ItemsControl)GetTemplateChild("SectionItems");

this.Loaded += new RoutedEventHandler(SectionSelectControl_Loaded);

}

void SectionSelectControl_Loaded(object sender, RoutedEventArgs e)

{

StackPanel sp = itemsControl.ItemsHost as StackPanel;

}

Hope this helps,
Jim (http://jimmangaly.blogspot.com/)

Please MARK the replies as answers if they answered your question

http://www.identitymine.com/

Jim Mangaly

Joined on 04-21-2008
Kochi, India
Posts 108
05-12-2008 6:19 AM
Re: Re: Re: Custom Control editing in code behind
Nice ill have to try that. Thank you. I ended up adding the ToggleButtons dynamically using a bind with the contents of a ContentControl. This allows me to keep track of the buttons easier as well for some stuff Ill need to do later. Not sure if this is the best way or not. It still allows me to use the xaml for the styles. I guess I could now use my current method or I could access the button through the panel they are on. Btw what tags do you use to post code.
valleyman86

Joined on 05-11-2008
Posts 3