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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:07:55+00:00 2026-05-15T16:07:55+00:00

I have a collection of objects stored in a CollectionViewSource and bound to a

  • 0

I have a collection of objects stored in a CollectionViewSource and bound to a DataGrid. I want to display a ‘detail view’ of the object currently selected in the DataGrid. I can obtain the current object using CollectionViewSource.View.CurrentItem.

MyClass
{
    [IsImportant]   
    AProperty{}

    AnotherProperty{}

    [IsImportant]
    YetAnotherProperty{}
}

What I would like to do is display a label (with the property name) and a control (for editing) in a listbox, for each of those properties marked with the IsImportant attribute. The binding must work between the edits made, the DataGrid and the backing object. The control displayed should vary based on the property’s type, which can either be boolean, string or IEnumerable<string> (I have written an IValueConverter to convert between enumerable and newline-delimited string).

Does anyone know of a method for accomplishing this? I can currently display the values of each property through the following, but editing them would not update the backing object:

listBox.ItemsSource = from p in typeof(MyClass).GetProperties()
                      where p.IsDefined(typeof(IsImportant), false)
                      select p.GetValue(_collectionViewSource.View.CurrentItem, null);

To clarify, I would like this to happen ‘automagically’, without manually specifying property names in the XAML. If I can dynamically add to the XAML at runtime based on which properties are marked with attributes, that would also be fine.

  • 1 1 Answer
  • 1 View
  • 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-15T16:07:56+00:00Added an answer on May 15, 2026 at 4:07 pm

    You want a control that has a label with the property name and control to edit the property value, so start by creating a class that wraps a property of a specific object to act as the DataContext for that control:

    public class PropertyValue
    {
        private PropertyInfo propertyInfo;
        private object baseObject;
    
        public PropertyValue(PropertyInfo propertyInfo, object baseObject)
        {
            this.propertyInfo = propertyInfo;
            this.baseObject = baseObject;
        }
    
        public string Name { get { return propertyInfo.Name; } }
    
        public Type PropertyType { get { return propertyInfo.PropertyType; } }
    
        public object Value
        {
            get { return propertyInfo.GetValue(baseObject, null); }
            set { propertyInfo.SetValue(baseObject, value, null); }
        }
    }
    

    You want to bind the ItemsSource of a ListBox to an object in order to populate it with these controls, so create an IValueConverter that will convert an object to a list of PropertyValue objects for its important properties:

    public class PropertyValueConverter
        : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return
                from p in value.GetType().GetProperties()
                where p.IsDefined(typeof(IsImportant), false)
                select new PropertyValue(p, value);
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }
    

    The final trick is that you want the edit control to vary based on the property’s type. You can do that by using a ContentControl and setting the ContentTemplate to one of various editor templates based on the property type. This example uses a CheckBox if the property is a Boolean and a TextBox otherwise:

    <DataTemplate x:Key="CheckBoxTemplate">
        <CheckBox IsChecked="{Binding Value}"/>
    </DataTemplate>
    <DataTemplate x:Key="TextBoxTemplate">
        <TextBox Text="{Binding Value}"/>
    </DataTemplate>
    <Style x:Key="EditControlStyle" TargetType="ContentControl">
        <Setter Property="ContentTemplate" Value="{StaticResource TextBoxTemplate}"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding PropertyType}" Value="{x:Type sys:Boolean}">
                <Setter Property="ContentTemplate" Value="{StaticResource CheckBoxTemplate}"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
    <DataTemplate DataType="{x:Type local:PropertyValue}">
        <StackPanel Orientation="Horizontal">
            <Label Content="{Binding Name}"/>
            <ContentControl Style="{StaticResource EditControlStyle}" Content="{Binding}"/>
        </StackPanel>
    </DataTemplate>
    

    Then, you can just create your ListBox as:

    <ItemsControl ItemsSource="{Binding Converter={StaticResource PropertyValueConverter}}"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sorted collection of objects (it can be either SortedList or SortedDictionary,
I have a new object with a collection of new objects within it on
I have a Task object that has a collection of Label objects ... in
I have a collection of objects to which I'd like to just add a
So I have a collection of objects. The exact type isn't important. From it
I have a collection of domain objects that I need to convert into another
What is the best way to have synchronized a collection of objects between various
I have a collection of custom entity objects one property of which is an
Suppose you have a collection of a few hundred in-memory objects and you need
I have a listbox that is databound to a Collection of objects. The listbox

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.