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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:48:31+00:00 2026-05-25T20:48:31+00:00

I’ve got a gridview in my wpf application, the xaml of which looks like

  • 0

I’ve got a gridview in my wpf application, the xaml of which looks like this:

   <ListView SelectionMode="Extended" ItemsSource="{Binding AllPartTypes}"
      local:DataGridService.SelectedItems="{Binding Path=SelectedPartTypes, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
        <ListView.View>
            <GridView>
                some columns...
            </GridView>
        </ListView.View>
   </ListView>

Here is the attached behavior I’m using to get the selected items:

public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.RegisterAttached(
            "SelectedItems",
            typeof(IList),
            typeof(DataGridService),
             new UIPropertyMetadata(new List<object>() as IList, OnSelectedItemsChanged));

        static SelectionChangedEventHandler GetSelectionChangedHandler(DependencyObject obj)
        {
            return (SelectionChangedEventHandler)obj.GetValue(SelectionChangedHandlerProperty);
        }
        static void SetSelectionChangedHandler(DependencyObject obj, SelectionChangedEventHandler value)
        {
            obj.SetValue(SelectionChangedHandlerProperty, value);
        }
        static readonly DependencyProperty SelectionChangedHandlerProperty =
            DependencyProperty.RegisterAttached("SelectionChangedHandler", typeof(SelectionChangedEventHandler),
            typeof(DataGridService), new UIPropertyMetadata(null));

        //d is MultiSelector (d as ListBox not supported)
        static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
        {
            if (GetSelectionChangedHandler(d) != null)
                return;

            if (d is MultiSelector)//DataGrid
            {
                MultiSelector multiselector = d as MultiSelector;
                SelectionChangedEventHandler selectionchanged = null;
                foreach (var selected in (d as DataGrid).SelectedItems) // GetSelectedItems(d) as IList)
                    multiselector.SelectedItems.Add(selected);

                selectionchanged = (sender, e) =>
                {
                    SetSelectedItems(d, multiselector.SelectedItems);
                };
                SetSelectionChangedHandler(d, selectionchanged);
                multiselector.SelectionChanged += GetSelectionChangedHandler(d);
            }
            else if (d is ListBox)
            {
                ListBox listbox = d as ListBox;
                SelectionChangedEventHandler selectionchanged = null;

                selectionchanged = (sender, e) =>
                {
                    SetSelectedItems(d, listbox.SelectedItems);
                };
                SetSelectionChangedHandler(d, selectionchanged);
                listbox.SelectionChanged += GetSelectionChangedHandler(d);
            }
        }

        public static IList GetSelectedItems(DependencyObject obj)
        {
            return (IList)obj.GetValue(SelectedItemsProperty);
        }

        public static void SetSelectedItems(DependencyObject obj, IList value)
        {
            obj.SetValue(SelectedItemsProperty, value);
        }

This works fine for getting the selected items in the viewmodel when the user clicks a button or something. My problem is that, when the screen loads, I need to highlight the items which were previously selected. I unsuccessfully tried setting the ‘SelectedPartTypes’ property in the constructor of the VM, assuming that the two-way binding would take care of it. Is there an easy way to show which items are selected when the control loads?

  • 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-25T20:48:32+00:00Added an answer on May 25, 2026 at 8:48 pm

    Can’t you bind the IsSelected property in the ListBoxItem Style?

    <Style TargetType="ListBoxItem">
        <Setter Property="IsSelected" Value="{Binding IsSelected}" />
    </Style>
    

    If your data objects don’t have a property to track selection, you may need to use a converter to return true if the current object is within SelectedPartTypes

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

Sidebar

Related Questions

I would like to run a str_replace or preg_replace which looks for certain words
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

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.