tags:

views:

931

answers:

2

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.

+2  A: 

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?

bendewey
I'm looking for a list with a border around each item, I updated the question to hopefully be more clear.
brendan
Then I would go with the template method describe in my answer. This way, you don't have to worry about appending a border element via code.
bendewey
ok, but there are other list items, in other list boxes in the same user control, that I do not want to apply this border to, how can I selectively apply this template?
brendan
So in order to accomidate this you can use the ListView.ItemContainerStyle key. Note. I changed your keynames to ListBoxItemRoundedBorder and RoundedBorder, since the ListBoxItemRoundedBorder wasn't a great name for a Border style.
bendewey
+1  A: 

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>

HTH, Kent

Kent Boogaart