Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Creating a control template with code behind

$
0
0

Hi I'm trying to mimic a XAML style I found for changing the color of selection in the listbox. The XAML works great but my code behind doesn't seem to work. When I go to select an item in the list box nothing is displayed(I'm expecting a Yellow overlay). I'm hoping someone will be able to see what I'm doing wrong if I show you the XAML and the code behind I'm using to mimic it.

<Stylex:Key="SimpleListBoxItem"TargetType="ListBoxItem"><SetterProperty="Template"><Setter.Value><ControlTemplateTargetType="ListBoxItem"><BorderName="Border"Padding="2"SnapsToDevicePixels="true"><ContentPresenter/></Border><ControlTemplate.Triggers><TriggerProperty="IsSelected"Value="true"><SetterTargetName="Border"Property="Background"Value="Yellow"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style>
 MyDataSetList.ItemContainerStyle = new Style();







 Setter setter = new Setter();



 setter.Property = ListBoxItem.FocusVisualStyleProperty;



 setter.Value = null;







 MyDataSetList.ItemContainerStyle.Setters.Add(setter);











 ControlTemplate template = new ControlTemplate(typeof(ListBoxItem));



 template.VisualTree = new FrameworkElementFactory(typeof(Border));



 template.VisualTree.SetValue(Border.PaddingProperty, new Thickness(6));



 template.VisualTree.SetValue(Border.SnapsToDevicePixelsProperty, true);



 template.VisualTree.AppendChild(new FrameworkElementFactory(typeof (ContentPresenter)));







 Trigger trigger = new Trigger();



 trigger.Property = ListBoxItem.IsSelectedProperty;



 trigger.Value = true;







 setter = new Setter();



 setter.TargetName = "Border";



 setter.Property = Border.BackgroundProperty;



 setter.Value = new SolidColorBrush(Colors.Yellow);







 template.Triggers.Add(trigger);



 setter = new Setter();



 setter.Property = ListBoxItem.TemplateProperty;



 setter.Value = template;



 MyDataSetList.ItemContainerStyle.Setters.Add(setter);




Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>