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 8549303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:41:29+00:00 2026-06-11T13:41:29+00:00

I have a WPF style that I am applying to list box items. Currently,

  • 0

I have a WPF style that I am applying to list box items. Currently, each item is bound to a KeyValuePair. I display the Key in a TextBlock inside the ListBoxItem:

<TextBlock Name="Label" Text="{Binding Key}" />

What I want to do is make the Style generic so that if the data is not a KeyValuePair, (maybe just a string), I can bind the data correctly to the TextBlock.

Is there a way to pass a parameter to a Style or DataTemplate or make the data binding generic?

My style:

<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="ListBoxItem">
        <Border x:Name="Border" Padding="2" SnapsToDevicePixels="true" CornerRadius="4" BorderThickness="1">
          <TextBlock Name="Label" Text="{Binding Key}" />
        </Border>
        <ControlTemplate.Triggers>
          <MultiTrigger>
            <MultiTrigger.Conditions>
              <Condition Property="IsSelected" Value="True"/>
            </MultiTrigger.Conditions>
            <Setter TargetName="Border" Property="Background" Value="{StaticResource SelectionGradient}" />
          </MultiTrigger>
        <ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>
  • 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-06-11T13:41:29+00:00Added an answer on June 11, 2026 at 1:41 pm

    Let me give you a quick example. Let’s say your TextBox contains a number, and you want it to be with a red Background if the number is negative, or with a green background if >= 0

    Here is the style you’d write:

    <TextBlock Text="{Binding Key}" >
      <TextBlock.Style>
        <Style TargetType="TextBox">
          <Setter Property="Background" Value="{Binding Key, Converter={StaticResource MyConverterResource}" />
        </Style>
      </TextBlock.Style>
    </TextBlock>
    

    Here is the converter you’d write:

    public class MyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double source = (double)value;
            return source < 0 ? Brushes.Red : Brushes.Green;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // We don't care about this one here
            throw new NotImplementedException();
        }
    }
    

    And, to access the converter in your xaml, you just have to do the following, assuming your converter is in the namespace MyNamespace:

    <Window xmlns:my="clr-namespace:MyNamespace">
      <Window.Resources>
        <my:MyConverter x:Key="MyConverterResource">
      </Window.Resources?
      <!-- Your XAML here -->
    </Window>
    

    (of course you can put this in any Resources, may it be a UserControl or whatever )
    This will allow you to call your converter by writing {StaticResource MyConverterResource}

    And here, you’d have a conditional style, the converter deciding which color to set as a background according to one parameter, in my case, the value itself (but it can be whatever you want)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF application with ListBox that display list of items. Each item
I'm trying to style Comboboxes in WPF so that they are white, and have
So I have this WPF style: <Style x:Key=SmallLinkButton TargetType=Button> <Setter Property=Template> <Setter.Value> <ControlTemplate TargetType=Button>
I have a WPF TextBlock bound to a string. If that string is empty,
I have a WPF application that I'm trying to dynamically add items to a
I'm currently implementing a listbox in WPF that will have 2 alternative layouts for
We have a scenario where we want to display a list of items and
I have a WPF style that sets TextDecorations dependency property for TargetType: TexBlock .
I have a WPF button style below: <Style x:Key=myButtonStyle TargetType={x:Type Button}> <Setter Property=Template> <Setter.Value>
I have WPF ContextMenu with MenuItems that support custom style. MenuItem is a ToggleButton.

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.