Wednesday, February 13, 2008

Troubleshooting WPF Designer Load Failures

Hi,

Now that people have been kicking the tires of the WPF Designer for Visual Studio 2008 (aka "Cider). Here's a preview of the topic that will ship in the docs. Hopefully this will help out before frustration and despair set in.

Thanks,
Venugopal.

Monday, February 4, 2008

How Can I Get ItemsPanelTemplate Obeject Reference In Custom Control Class Itself?.

Hi,

I have faced complex problem during my ScrollPanel(I will post this ScrollPanel logic and advantage by shortly) CustomControl implementation. The actual problem is I couldn't get ItemsPanelTemplate object reference in control class itlsef. I found some solution in net, but those things are related to outside of control class only.

Then finally i found the soultion with the help of VisualTreeHelper class. Here is the code snippet.

private T FindItemsPanel(Visual visual)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++)
{
Visual child = VisualTreeHelper.GetChild(visual, i) as Visual;
if (child != null)
{
if (child is T && VisualTreeHelper.GetParent(child) is ItemsPresenter)
{
object temp = child;
return (T)temp;
}


T panel = FindItemsPanel(child);
if (panel != null)
{
object temp = panel;
return (T)temp; // return the panel up the call stack
}
}
}
return default(T);
}

But here also I had one problem like getting object reference after initialized my control. I had tried with OnInitialized method, and tried in constructor of my control class, but not use. Then finally found the solution with the help of OnLoaded event of my control class.

Like Below :

public ReflectionGallery()
{
this.Loaded += new RoutedEventHandler(ReflectionGallery_Loaded);
}



void ReflectionGallery_Loaded(object sender, RoutedEventArgs e)
{

//ItemsPanelObject is my own dependency property
ItemsPanelObject = FindItemsPanel(this);
}


This is my control template(Style)

<Style TargetType="{x:Type local:ReflectionGallery}" x:Key="{x:Type local:ReflectionGallery}">
<Setter Property="Template" Value="{StaticResource ReflectionGalleryTemplateKey}"/>
<Setter Property="Margin" Value="2"/>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<local:ReflectionPanel Name="PART_ReflectionPanel" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>


Now we can use ItemsPanelObject property to play with custom panel of my ScrollPanel control.


I hope this will help to Custom Control authors like me :) :).


Please feel free to post your comments on this.

I will meet you with my ScrollPanel post. This might be very cool like Carousel panel.

Thanks,
Venugopal.