0

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.

3 Answers 3

2

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?

4
  • I'm looking for a list with a border around each item, I updated the question to hopefully be more clear.
    – brendan
    Mar 30, 2009 at 18:42
  • 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
    Mar 30, 2009 at 19:19
  • 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
    Mar 30, 2009 at 20:01
  • 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
    Mar 30, 2009 at 20:12
2

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>
1

"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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.