Skip to main content

Microsoft Silverlight

Answered Question Accessing a style defined in xaml in the code behind.RSS Feed

(0)

something relevant
somethin...

Member

Member

6 points

34 Posts

Accessing a style defined in xaml in the code behind.

Hi,

I've defined a style for a listboxitem in xaml and i need to apply this style in codebehind.  This is what I've tried:

 

xaml:

<UserControl.Resources>

<Style TargetType="ListBoxItem" x:Key="draggableItemStyle">

<Setter Property="HorizontalAlignment" Value="Stretch" />

<Setter Property="HorizontalContentAlignment" Value="Stretch" />

<Setter Property="dragDrop:RadDragAndDropManager.AllowDrag" Value="True" />

</Style>

</UserControl.Resources>

 

xaml.cs:

System.Windows.Style draggableItemStyle=GetTemplateChild("draggableItemStyle") as System.Windows.Style;

listbox.ItemContainerStyle = draggableItemStyle;

 

The dragable style on the listbox doesnt seem to work.  Is this the correct way of doing it or is there another way?

Any help would really be appreciated.

Thanks and regards,

sasideva
sasideva

Member

Member

240 points

58 Posts

Answered Question

Re: Accessing a style defined in xaml in the code behind.

button.Style = Application.Current.Resources["TopMenuItemButton"] as Style; // If your style is defined in App.xaml Application.Resources section
button.Style = this.Resources[StyleKey] as Style; // if your style is defined in Page.XAML UserControl.Resources section
button.Style = button.Resources[StyleKey] as Style; // if your style is defined in the XAML as control.Resouces section

Regards,
Deva
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities