I am working on some XAML for a wpf application and I am having some trouble getting it to do what I want. Here is a sample of my XAML:
<!-- Tool Bar Tray -->
<ToolBarTray Name="toolBarTray1" DockPanel.Dock="Top">
<!-- File And Edit Tools -->
<ToolBar Name="toolBar1" Band="1" BandIndex="1">
<!-- Regular Items -->
<Button>A</Button>
<Button>B</Button>
<!-- Overflow Menu For Special Items -->
<MenuItem ToolBar.OverflowMode="Always" Header="Special Items">
<MenuItem Header="C"/>
<MenuItem Header="D"/>
</MenuItem>
</ToolBar>
</ToolBarTray>
When I click on the overflow button of my toolbar, the "Special Items" MenuItem appears with a little arrow next to it, indicating nested elements. However, when I hover the mouse over "Special Items" or try to click on it, the MenuItems "C" and "D" are not being displayed.
I was hoping that MenuItem would just work outside of a Menu, but I tried to do the straight-forward thing, just in case. Including these MenuItems inside a Menu and, instead, giving this Menu the ToolBar.OverflowMode="Always" property produces some unwanted styling. The arrow is no longer present, the "Special Items" entry needs to be clicked on to activate the sub-menu and the sub-menu positioning looks a little off.
Does anyone know what is going on?
Edit: Adding a menu to the overflow is producing exactly what I requested (big surprise). What I am after is a way to convert top-level headers and items to the sub-menu level. I have turned towards this control template example on MSDN for a solution (below).
Edit,Edit:
#gcores (comment discussion): Really? Am I missing something?
<ToolBar Name="toolBar1" Band="1" BandIndex="4">
<!-- Displayed Buttons -->
<Button>A</Button>
<Button>B</Button>
<!-- Special Items Menu -->
<Menu ToolBar.OverflowMode="Always" >
<MenuItem Style="{StaticResource MenuItemStyle}" Header="Special">
<MenuItem Header="C"/>
<MenuItem Header="D"/>
</MenuItem>
</Menu>
</ToolBar>
This snippet doesn't work for me. I have to click on 'Special' for the sub-menu to display.
Another solution is to use the existing templates and override the Template for the TopLevelHeader with the Template of the SubmenuHeader.
<Style x:Key="MenuItemStyle" TargetType="{x:Type MenuItem}">
<Style.Triggers>
<Trigger Property="Role" Value="TopLevelHeader">
<Setter Property="Template"
Value="{StaticResource {x:Static MenuItem.SubmenuHeaderTemplateKey}}"/>
</Trigger>
</Style.Triggers>
</Style>
And use this style in your top level MenuItem. That should simplify your code.
EDIT:
You're right, it only works when you click on it (don't know how I convinced myself it worked, sorry :)).
It's functionality is like a TopLevelMenu even though the Template says otherwise, its quite confusing.
Only thing I can think of is adding a Trigger to show the Submenu on IsMenuOver and handling the Click event so it does nothing but I don't know how well that would work.
After more reading, a solution I am using is below.
<!-- Resource Dictionary Stuff -->
<!-- Some Brushes -->
<SolidColorBrush x:Key="Brush_1"
Color="White" />
<LinearGradientBrush x:Key="Brush_2"
StartPoint="0 0"
EndPoint="0 1">
<GradientStop
Color="White"
Offset="0"/>
<GradientStop
Color="DarkSeaGreen"
Offset="1"/>
</LinearGradientBrush>
<SolidColorBrush x:Key="Brush_3"
Color="DarkOliveGreen"/>
<!-- Custom MenuItem - Top Level Header - Style 1 -->
<Style x:Key="MenuItem_TLH_Style1"
TargetType="MenuItem">
<!--<EventSetter Event="PreviewMouseDown" Handler="DoNothing"/>-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate x:Name="ControlTemplate"
TargetType="MenuItem">
<!-- A headered text that may display a submenu
on a trigger. This submenu is the host for a
menu item's items. -->
<Border x:Name="BoundaryBorder"
Background="{StaticResource Brush_1}"
BorderThickness="1">
<Grid x:Name="ContainerGrid">
<ContentPresenter x:Name="HeaderContent"
Margin="6 3 6 3"
ContentSource="Header"
RecognizesAccessKey="True"/>
<Popup x:Name="SubmenuPopup"
Placement="Bottom"
IsOpen="{TemplateBinding IsSubmenuOpen}"
AllowsTransparency="True"
Focusable="False"
PopupAnimation="Fade">
<Border x:Name="SubmenuBoundaryBorder"
SnapsToDevicePixels="True"
Background="{StaticResource Brush_1}"
BorderBrush="{StaticResource SolidBorderBrush}"
BorderThickness="1">
<StackPanel x:Name="ItemsStackPanel"
IsItemsHost="True"
KeyboardNavigation.DirectionalNavigation="Cycle"/>
</Border>
</Popup>
</Grid>
</Border>
<ControlTemplate.Triggers>
<!-- -->
<Trigger
Property="IsSuspendingPopupAnimation"
Value="true">
<Setter
TargetName="SubmenuPopup"
Property="PopupAnimation"
Value="Fade"/>
</Trigger>
<!-- On mouse-over, show the submenu and highlight the header. -->
<Trigger
Property="IsMouseOver"
Value="true">
<Setter
TargetName="BoundaryBorder"
Property="Background"
Value="{StaticResource Brush_2}"/>
<Setter
TargetName="BoundaryBorder"
Property="BorderBrush"
Value="{StaticResource Brush_3}"/>
<Setter
Property="IsSubmenuOpen"
Value="true"/>
<!-- sloppy? -->
<Setter
TargetName="SubmenuPopup"
Property="IsOpen"
Value="true"/>
</Trigger>
<Trigger
SourceName="SubmenuPopup"
Property="AllowsTransparency"
Value="true">
<Setter
TargetName="SubmenuBoundaryBorder"
Property="CornerRadius"
Value="0 0 4 4"/>
<Setter
TargetName="SubmenuBoundaryBorder"
Property="Padding"
Value="0 0 0 3"/>
</Trigger>
<!-- Visually indicate an unaccessible menu item. -->
<Trigger
Property="IsEnabled"
Value="false">
<Setter
Property="Foreground"
Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- ... -->
<!-- Inside a window XAML file -->
<!-- Tool Bar Tray -->
<ToolBarTray x:Name="toolBarTray1"
DockPanel.Dock="Top">
<!-- File And Edit Tools -->
<ToolBar x:Name="toolBar1"
Band="1" BandIndex="1">
<!-- Displayed Buttons -->
<Button x:Name="ButtonA"
Content="A"/>
<Button x:Name="ButtonB"
Content="B"/>
<!-- Overflow Menu For Special Items -->
<Menu x:Name="OverflowMenu"
ToolBar.OverflowMode="Always">
<MenuItem x:Name="SpecialsMenuItem"
Style="{StaticResource MyStyle}"
Header="Special Items">
<MenuItem x:Name="CMenuItem"
Header="C">
<MenuItem x:Name="DMenuItem"
Header="D"/>
</MenuItem>
</MenuItem>
</Menu>
</ToolBar>
</ToolBarTray>
I attack the behavior of 'SubmenuPopup' on a mouse-over, rather than handling the click event. I'd like to understand this more fully, so I tried commenting out this part of the trigger and adding an event handler that calls a 'DoNothing()' method on the 'PreviewMouseDown' event. It turns out that I am missing something and I think it is related to focusing and/or how a menu handles its items collection. Not allowing an event to propagate after 'DoNothing()' (routedEventArgs.Handled = true) seems to eliminate the problems when clicking on the "Special Items" menu item. However, if one navigated away from the menu or added another menu item and then clicked on that, the hover behavior can be turned off or switched on and off.
The only way I could find to even come close to generating this behaviour was to create a menu in the overflow which contained a single menu item whose header was itself another menu item called "Special Items" and containing the proper children. It worked as intended but looked bizarre (this could be remedied by custom templates) and also seems like a huge hack. The only "proper" way to do this that I can think of would be making your own MenuItem-like control which pops up a ContextMenu or Popup when hovered upon, since I don't think that a custom ControlTemplate can change the default behaviour of a menu so as not to require a click on the top level item.
Related
I'm trying to desinge a Template for a Button, which addapts the shape of the Button. I want to give the shape as Attribute to the control. Like this:
<Button Style="{StaticResource MyStyle}">
<Button.Shape>
<ImageBrush ImageSource="MyImage"/>
</Button.Shape>
</Button>
I tried to use the OpacityMask-Attribute of the Button, but somehow I coulden't specify in the template on which element I want to apply the mask.
<ControlTemplate x:Key="Test" TargetType="Button">
<Grid Background="{TemplateBinding Background}"
SnapsToDevicePixels="True">
<Rectangle Fill="{TemplateBinding Foreground}" OpacityMask"{TemplateBinding OpacityMask}">
</Rectangle>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="#aaff00ff"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
I already tried something like this:
<Button Style="{StaticResource MyStyle}">
<Button.OpacityMask>
<ImageBrush ImageSource="MyImage"/>
</Button.OpacityMask>
</Button>
But then i can't change the background anymore. I know, I could put every button in a Grid with the background I want, but I try to learn the concept of Templates and I wonder if there is a more elegant solution. :)
I'm using a resource dictionary in my wpf proyect.
The dictionary has these 2 styles:
<Style x:Key="MyMenu" TargetType="Menu">
<Setter Property="Background" Value="Black"/>
<Setter Property="Foreground" Value="White"/>
</Style>
<Style x:Key="MyToolbar" TargetType="ToolBar">
<Setter Property="Background" Value="Black"/>
</Style>
And in my XAML file, I use these 2 styles as it follows:
For the menu:
<Menu Name="menuMainBar" Style="{DynamicResource MyMenu}" IsMainMenu="True" VerticalAlignment="Top" Margin="0,10,0,0">
<MenuItem ...
</Menu>
For the toolbar:
<ToolBarTray Name="toolBarTrayRigth_wargames" Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50">
<ToolBar Name="toolBarRigth_wargames" Style="{StaticResource MyToolbar}" BorderThickness="0,0,1,0">
<Button ...
</ToolBar>
</ToolBarTray>
This is the result:
As you can notice, the menu takes the style correctly, but the toolbar isn't affected by the style at all.
I tried removing all the style elements from the toolbartray tag, as it follows:
<ToolBarTray Name="toolBarTrayRigth_wargames">
...
</ToolBarTray>
And now the toolbar works, but as you can see, now the toolbarTray is not how I want it to be (I want a vertical toolbar, not a horizontal toolbar):
I also tried setting the background of the ToolBar directly in the xaml file:
<ToolBarTray Name="toolBarTrayRigth_wargames" Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50">
<ToolBar Name="toolBarRigth_wargames" BorderThickness="0,0,1,0" Background="Black">
<Button ...
</ToolBar>
</ToolBarTray>
...and it works:
But I don't want to do that, I want to use a resource dictionary for my toolbar's style.
any ideas on how to achieve that?
I found, bizarrely, that if I set the Orientation of the parent ToolBarTray to Horizontal, that your ToolBar style was able to set the background on the ToolBar.
I also found that with the ToolBarTray orientation still being Vertical, this worked to set the ToolBar's background:
<Style x:Key="MyToolbar" TargetType="ToolBar">
<Style.Triggers>
<!--
This is intentional. A conventional setter was found not to set the background
when the parent ToolBarTray's Orientation was Vertical.
-->
<DataTrigger Binding="{Binding Source={x:Null}}" Value="{x:Null}">
<Setter Property="Background" Value="Black" />
</DataTrigger>
</Style.Triggers>
</Style>
I find this very strange.
as #EdPlunkett found, its a strange Setter in the default template in the Trigger for Vertical Orientation:
<Trigger Property="Orientation" Value="Vertical">
<Setter Property="Margin" TargetName="Grid" Value="1,3,1,1"/>
<Setter Property="Style" TargetName="OverflowButton">
<Setter.Value>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Background" Value="#FFEEF5FD"/>
you can edit the template (in Document Outline panel, right click on the ToolBar element > Edit Template > Edit a Copy...), and remove this Setter.
I have Style that applies to all of the buttons of my application:
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontSize" Value="16" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Ellipse x:Name="StatusButtonCircle" Stroke="Black" StrokeThickness="0" Fill="AliceBlue" Stretch="Uniform">
<Ellipse.Width>
<Binding ElementName="StatusButtonCircle" Path="ActualHeight"/>
</Ellipse.Width>
</Ellipse>
<Ellipse x:Name="StatusButtonCircleHighlight" Margin="4" Stroke="Black" StrokeThickness="2" Stretch="Uniform">
<Ellipse.Width>
<Binding ElementName="StatusButtonCircleHighlight" Path="ActualHeight"/>
</Ellipse.Width>
</Ellipse>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
<ControlTemplate.Triggers>
... some Triggers here
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
How can I change properties (e.g. FontWeight, FontSize etc.) in XAML? I tried this:
<Button FontWeight="Bold" FontSize="30" Foreground="Red">
</Button>
In the designer-view, I see the changes. But during runtime those changes are not applied.
After some investigation, I also have a Style for all TextBlock like this:
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="16" />
<Setter Property="FontFamily" Value="Segoe UI Semibold" />
<Setter Property="Foreground" Value="White" />
</Style>
This Style seems to override the TextBlock that is used on the Button. I still can't change the Text Properties in XAML.
Here's what it looks like if I use the Styles above in an empty project:
In the designer, the changes are applied, during runtime the one from the TextBlock are applied. If I assign a x:Key to the TextBlock, it works fine. But then I have to assign this Style to every TextBlock used in the app manually.
You are facing typical style inheritance issue in wpf.
A control looks for its style at the point when it is being initalized. The way the controls look for their style is by moving upwards in logical tree and asking the logical parent if there is appropriate style for them stored in parent's resources dictionary.
In your case, you are using ContentPresenter in button as a default behaviour. and it is using TextBlock to represent text in button by default.
Therefore at the time of initialization, ContentPresenter finding TextBlock style and applying to represent content in button.
If you want to restrict ContentPresenter to look for the style then you have to bind a blank style to content presenter so that it will not look for any further style.
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontSize" Value="16" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Ellipse x:Name="StatusButtonCircle" Stroke="Black" StrokeThickness="0" Fill="AliceBlue" Stretch="Uniform">
<Ellipse.Width>
<Binding ElementName="StatusButtonCircle" Path="ActualHeight"/>
</Ellipse.Width>
</Ellipse>
<Ellipse x:Name="StatusButtonCircleHighlight" Margin="4" Stroke="Black" StrokeThickness="2" Stretch="Uniform">
<Ellipse.Width>
<Binding ElementName="StatusButtonCircleHighlight" Path="ActualHeight"/>
</Ellipse.Width>
</Ellipse>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center">
<ContentPresenter.Resources>
<Style TargetType="TextBlock" BasedOn="{x:Null}"/>
<!-- Assigned Blank style here therefore it will not search for any further style-->
</ContentPresenter.Resources>
</ContentPresenter>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
You can do it with the BasedOn. I show you an example.
<Window.Resources>
<Style TargetType="ToggleButton" BasedOn="{StaticResource DefToggleButton}">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Content" Value="Some Cool Stuff"/>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content" Value="More Stuff"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
Here in my resources I have DefToggleButton, now in my xaml file I can set up any Property according to my need (which in this case is the FontWeight and Content Property).
I think if you remove the Template from your Style, then you can do what you want to do, like this:
<Window.Resources>
<Style TargetType="Button" x:Key="stBtn>
<Setter Property="Background" Value="Blue" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FontFamily" Value="Segoe UI Semibold" />
</Style>
</Window.Resources>
The Template that you have says, that all Buttons should be shown as a Border with a ContentPresenter inside, which is not what you have asked.
Without the Template, you can define your Buttons like this:
<Button Content="Hi!" Style="{StaticResource stBtn}" Foreground="Red" >
Like this, you have a Blue Button with Red Foreground.
=================
Edit
So what if you define a Template, and use it in you style, like this?
Then, by TemplateBinding you can define that the Foreground and teh Content come later, when the Button is actually defined.
<Window.Resources>
<ControlTemplate x:Key="ctBtn" TargetType="{x:Type Button}">
<Label Background="Green" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}"/>
</ControlTemplate>
<Style x:Key="stBtn2" TargetType="{x:Type Button}">
<Setter Property="Template"
Value="{StaticResource ctBtn}" />
</Style>
<Window.Resources>
Then by defining the Button:
<Button Content="Hi!" Style="{StaticResource stBtn2}" Foreground="Red" >
===============
Edit2
So the general idea is that you can define a TemplateBinding for the properties of the elements in your template. So for example,you have an Ellipse in your template:
<Ellipse Fill="{TemplateBinding BorderBrush}" />
This defines that the Fill property of your Ellipse comes from the BorderBrush of your Button (Assuming that the template is targeting a Button)
Accordingly, you can put a Label in your Template, and set a TemplateBinding for its Forground and FontWeight property.
<Label Foreground="{TemplateBinding Foreground}" />
First, for this issue to be reproduced, Styles need to be set within a ResourceDictionary which is then added to Application.Resources (precisellyTextBlock global style). Setting Styles within for example Window.Resources will not reproduce the issue.
Global TextBlock Style is applied to the TextBlock created by ConentPresenter
As noticed in the question, the issue is that the global (keyless) Style for TextBlock is applied to the TextBlock created by ContentPresenter when it concludes the content to display is a string. For some reason this doesn't happen when that Style is defined within Window.Resources. As it turns out, there is more to this than just "controls are looking for their styles within their parent's resources".
ControlTemplate is a boundary for elements not deriving from Control class
For TextBlock (which doesn't derive from Control class, but from UIElement) within ControlTemplate, it means that wpf will not look for it's implicit Style beyond it's templated parent. So it won't look for implicit Style within it's parent's resources, it will apply application level implicit Style found within Application.Resources.
This is by design (hardcoded into FrameworkElement if you will), and the reason is exactly to prevent issues like this one. Let's say you're creating a specific Button design (as you are) and you want all buttons in your application to use that design, even buttons within other ControlTemplates. Well, they can, as Button does derive from Control. On the other hand, you don't want all controls that use TextBlock to render text, to apply the implicit TextBlock Style. You will hit the same issue with ComboBox, Label... as they all use TextBlock, not just Button.
So the conclusion is: do not define global Style for elements which don't derive from Control class within Application.Resources, unless you are 100% sure that is what you want (move it to Window.Resources for example). Or, to quote a comment I found in source code for MahApps.Metro UI library: "never ever make a default Style for TextBlock in App.xaml!!!". You could use some solution to style the TextBlock within your Button's ControlTemplate, but then you'll have to do it for Label, ComboBox, etc... So, just don't.
I am trying to make a button in WPF that, when hovered by the mouse, lights up and gets the blue-ish selection around it. I managed the former, but by changing the button image, I apparently override the commands that highlights the button with a blue selection.
This is what I have:
<Button Command="DoSomething" Name="button">
<Button.Template>
<ControlTemplate>
<Image Height="32" Width="32" Stretch="Uniform" Name="buttonImage">
<Image.Style>
<Style TargetType="Image">
<Setter Property="Source" Value="/Project;component/Project/Bitmaps/Icon_colour.png" />
</Style>
</Image.Style>
</Image>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="false">
<Setter Property="Source" Value="/Project;component/Project/Bitmaps/Icon_grey.png" TargetName="buttonImage"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
Is there a way to get the selection back again, while keeping the icon-light-up effect?
You have replaced the default Button ControlTemplate, so all you need to do is to 'replace' the missing part(s) from the original ControlTemplate. You can find that in the Button Styles and Templates page on MSDN.
UPDATE >>>
Alternatively, you can simply add an Image into the Button.Content property:
<Button Command="{Binding DoSomething, Mode=OneWay}">
<Image Source="/Project;component/Project/Bitmaps/Icon_colour.png" />
</Button>
In my XAML I want to dynamically generate a ListBox with the following:
<ListBox Name="MainListBox">
<Border Style="{DynamicResource ListBoxItemRoundedBorder}">
<ListBoxItem >
<TextBlock>
Some Text Here
</TextBlock>
</ListBoxItem>
</Border>
<Border Style="{DynamicResource ListBoxItemRoundedBorder}">
<ListBoxItem >
<TextBlock>
Some Text Here
</TextBlock>
</ListBoxItem>
</Border>
<Border Style="{DynamicResource ListBoxItemRoundedBorder}">
<ListBoxItem >
<TextBlock>
Some Text Here
</TextBlock>
</ListBoxItem>
</Border>
</ListBox>
I want to add items to this listbox via code behind. How can I add the item and the border via code behind. I can add the list box items easy enough but can't seem to figure out the border:
For Each s As String in MyArray
Dim lbi as New ListBoxItem()
Dim tb as New TextBlock()
tb.Text = s
lbi.content = tb
MainListBox.Items.Add(lbi)
Next
Edit: To clear up any confusion I want a border around each of the ListBox Items. I've updated the XAML - effectively I want to render that XAML dynamically, or equivalent, via code behind. I already have the border style defined.
Have you looked in to Templating the ListBoxItem
Use this to get the border effect you're looking for
<Style x:Key="ListBoxItemRoundedBorder" TargetType="ListBoxItem">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border
Name="Border"
Padding="2"
SnapsToDevicePixels="true" Style="{DynamicResource RoundedBorder}">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background"
Value="{StaticResource SelectedBackgroundBrush}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground"
Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Then on your listbox use
<ListView ItemContainerStyle="{StaticResource ListBoxItemRoundedBorder}" />
Although, based on your question, I can't exactly see what design your looking for. Are you looking for a List with a border around it or a list with a border around each item?
I don't understand. If you want one Border, why not just stick it on the outside of the ListBox? I'll assume you want one Border per ListBoxItem. In that case, just modify the ItemTemplate:
<ListBox>
<ListBox.ItemTemplate>
<Border>
<TextBlock Text="{Binding}"/>
</Border>
</ListBox.ItemTemplate>
</ListBox>
"I want to add items to this listbox via code behind."
I have just built a page that loads controls to the page dynamically (based on a collection).
So to answer the the question... you must apply the settings (like what is done with the templating in xaml) in your code. Here is an example in C#: (in vb the first line would start with Dim listBoxStyle as Style...)
Style listBoxStyle = new System.Windows.Style(typeof(ListBox));
listBoxStyle.Setters.Add(new Setter(ListBox.BorderThicknessProperty, new Thickness(0,0,0,0)));
ListBox rdoList = new ListBox();
rdoList.Resources.Add(typeof(ListBox), listBoxStyle);
Notice the thickness(). I have mine set to no border as it defaults to having a border. You can do this with your textboxes and just add the thickness like (1,1,1,1).
Don't know how your calling your dynamic controls from code but you may want to view this post for an easy way to access dynamic wpf controls by name value from code.