Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 893749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:16:13+00:00 2026-05-15T14:16:13+00:00

I want do something like this. <Style TargetType={x:Type ListBoxItem} > <Setter Property=Style> <Setter.Value> <Border

  • 0

I want do something like this.

    <Style TargetType="{x:Type ListBoxItem}"  >
    <Setter Property="Style">
        <Setter.Value>
                <Border Style="{StaticResource BorderStyle}" Width="200" >
                </Border>
        </Setter.Value>
    </Setter>

   </Style>
    <Style x:Key="BorderStyle" TargetType="{x:Type Border}">
    <Setter Property="Background" Value="{StaticResource BackBrush}" />
    <Setter Property="BorderBrush" Value="Black" />
    <Setter Property="BorderThickness" Value="0.5" />
    <Setter Property="CornerRadius" Value="4" />
    <Setter Property="Margin" Value="4" />
    <Setter Property="Padding" Value="4" />
   </Style>

But it gives next error

Cannot add content of type ‘System.Windows.Controls.Border’ to an object of type ‘System.Object’.

and the code which use it

        for (int i = 0; i < 10; i++)
        {
            ListBoxItem lbItem = new ListBoxItem();
            lbItem.Content = "Item" + i;
            lb1.Add(lbItem);


        }

where “lb1” is my ListBox in xaml form

How can i give ListBoxItemStyle properly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:16:15+00:00Added an answer on May 15, 2026 at 2:16 pm

    It looks like you’re confused about the semantics of XAML. Until you get more used to XAML it might help to think about it as the C# equivalent. This is essentially what you’re doing right now:

        Style BorderStyle = new Style();
        Border inlineStyle = new Border { Style = BorderStyle };
        Style listBoxItemDefaultStyle = new Style();
        listBoxItemDefaultStyle.Setters.Add(new Setter(StyleProperty, inlineStyle));
        ListBoxItem item = new ListBoxItem { Style = listBoxItemDefaultStyle };
    

    One issue is that you’re setting a Style for the ListBoxItem from a Setter inside the Style for your ListBoxItem, which is of course going to cause some sort of problem with recursion. So removing that extra Style from the code we get:

        Style BorderStyle = new Style();
        Border inlineStyle = new Border { Style = BorderStyle };
        ListBoxItem item = new ListBoxItem { Style = inlineStyle };
    

    This is invalid because it’s trying to set a Style property (of Type Style) to a Border object. This is essentially what’s at the core of the error you’re seeing.

    What you really want in this case is to change the ListBoxItem ControlTemplate to incorporate your Border Style. This is the default Style modified to use your Border instead of the standard one using TemplateBindings to set its properties:

    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
        <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
        <Setter Property="Padding" Value="2,0,0,0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border x:Name="Bd" Style="{StaticResource BorderStyle}" Width="200">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="true"/>
                                <Condition Property="Selector.IsSelectionActive" Value="false"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                        </MultiTrigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to do something like this... <Style x:Key="MyBorderStyle" TargetType="Border"> <Setter Property="Padding" Value="{TemplateBinding Padding}"
When i write something like this: <Style x:Key=panelS> <Setter Property=Orientation Value=Horizontal /> <Setter Property=DockPanel.Dock
I want to do something like this: <style type=text/css> body { font-family: dialog-font; /*
I want to do something like this: Resource Dictionary <Color x:Key=clrPrimary>#5381ac</Color> <Color x:Key=clrSecondary>#20558a</Color> <Style
I want to use a global Style in WP7, something like: <Style TargetType=Button> //some
I want to do something like this: View <td class=status_@Model.IsActive> @Model.Description </td> CSS <style
I want something like this: abcdab.search(/a/g) //return [0,4] Is it possible?
I want to parse something like this: Hi [{tagname:content}] [{tag1:xnkudfdhkfujhkdjki diidfo now nested tag
I want to do something like this: require 'erb' @var = 'test' template =
I want to run something like this: For a = 0 To 4 For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.