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

  • Home
  • SEARCH
  • 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 655069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:33:47+00:00 2026-05-13T22:33:47+00:00

I’ve noticed a behavior difference between static and dynamic resource on ComboBox.ItemsSource, when the

  • 0

I’ve noticed a behavior difference between static and dynamic resource on ComboBox.ItemsSource, when the ComboBox gets out the visual tree.

  • in the static exemple the selected destination remains
  • in the dynamic exemple, the underlying object gets a null value

Binding seems OK, because when the comboboxes gets in focus, and have their SelectedIndex changed, the change are properly notified to the other list – both objects implements INotifyProperty – and both List are ObservableCollections.

It’s when the dynamic-bound combobox gets out of focus that the strange things happen

XAML

<Window ... xmlns:me = "clr-namespace:WpfComboBoxBug">
    <Window.Resources>
        <me:ShippingList x:Key="sl" />
        <me:DestinationList x:Key="dl" />
    </Window.Resources>
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="21" />
            <RowDefinition Height="421*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Custom:DataGrid Grid.Row="1"
            ItemsSource="{StaticResource sl}" x:Name="dg" AutoGenerateColumns="False" Grid.RowSpan="2">
        <Custom:DataGrid.Columns>
            <Custom:DataGridTextColumn Header="Reference" Binding="{Binding Reference}" />
            <Custom:DataGridTemplateColumn Header="Destination">
                <Custom:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Destination.Name}"></TextBlock>
                </DataTemplate>
                </Custom:DataGridTemplateColumn.CellTemplate>
                <Custom:DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{StaticResource dl}" SelectedItem="{Binding Destination,Mode=TwoWay}" DisplayMemberPath="Name"/>
                </DataTemplate>
                </Custom:DataGridTemplateColumn.CellEditingTemplate>
            </Custom:DataGridTemplateColumn>
        </Custom:DataGrid.Columns>
        </Custom:DataGrid>
            <Custom:DataGrid Grid.Column="1" Grid.Row="1" ItemsSource="{StaticResource sl}" x:Name="dg2" AutoGenerateColumns="False" Grid.RowSpan="2">
            <Custom:DataGrid.Columns>
                <Custom:DataGridTextColumn Header="Reference" Binding="{Binding Reference}" />
                <Custom:DataGridTemplateColumn Header="Destination">
                    <Custom:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Destination.Name}"></TextBlock>
                        </DataTemplate>
                    </Custom:DataGridTemplateColumn.CellTemplate>
                    <Custom:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{DynamicResource dynamicdl}" SelectedItem="{Binding Destination,Mode=TwoWay}" DisplayMemberPath="Name"/>
                        </DataTemplate>
                    </Custom:DataGridTemplateColumn.CellEditingTemplate>
                </Custom:DataGridTemplateColumn>
            </Custom:DataGrid.Columns>
        </Custom:DataGrid>
        <TextBox Height="23"  Name="textBox1" VerticalAlignment="Top" Text="Static" />
        <TextBox Height="23"  Name="textBox2"  VerticalAlignment="Top" Text="Dynamic" Grid.Column="2" />
    </Grid>
</Window>

CS

using System;
/* snip */

namespace WpfComboBoxBug
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            ShippingList sl;
            this.InitializeComponent();
            sl = this.Resources["sl"] as ShippingList;

            ResourceDictionary rd = new ResourceDictionary();
            rd.Add("dynamicdl", this.FindResource("dl"));

            dg2.Resources = rd;


            dg.ItemsSource = CollectionViewSource.GetDefaultView(sl);
            dg2.ItemsSource = CollectionViewSource.GetDefaultView(sl);
        }
    }
}

full source code at : http://dl.free.fr/eI1VtkaB8 ( VS 2008 SP1, .NET 3.5 SP1 )

I expected the dynamic resource to behave like the static resource in this case, beacause I intialize it once in the beginning.

  • Have I found a bug here ?
  • If that’ not the case, how would you explain the difference ?
  • 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-13T22:33:48+00:00Added an answer on May 13, 2026 at 10:33 pm

    I’d have to look at your code to be sure, but I would guess that your ComboBox has its SelectedItem or SelectedValue two-way bound to a property.

    When you use StaticResource the resource reference is resolved at XAML load time. When you use DynamicResource the resource refrence is resolved later. So what is probably happening is that your ComboBox is starting up with no items, which is forcing its SelectedItem and SelectedValue null. The two-way binding causes the property to update with this value.

    Personally I consider ComboBox’s inability to gracefully handle this situation to be a bug in the design of ComboBox, not an implementation bug.

    For my own projects I frequently use a ComboBox and ListBox enhancement I created to fix this problem: I have additional properties I can use in place of SelectedValue and SelectedItems. My new properties accepts any value until the ItemsSource is set, after which it stays synchronized with SelectedValue or SelectedItem.

    You could use a similar technique, or just always make sure ItemsSource is bound / initialized before SelectedValue or SelectedItem.

    Update

    When the control is removed from the visual tree, everything happens in reverse: ItemsSource is cleared immediately due to the ancestry change, then DataContext is cleared. During the interim, ComboBox has a null SelectedItem which is propagated to the bound property.

    An enhanced ComboBox or ListBox class with additional SelectedItem and SelectedValue properites can also solve this: It should keep SelectedItem / SelecteValue in sync with the custom properties whenever ItemsSource is non-null and decouple them whenever ItemsSource is null.

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

Sidebar

Ask A Question

Stats

  • Questions 530k
  • Answers 530k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer try this http://developer.android.com/reference/android/widget/MediaController.html#show() May 16, 2026 at 11:39 pm
  • Editorial Team
    Editorial Team added an answer Is your code accurate? It doesn't look like you're using… May 16, 2026 at 11:39 pm
  • Editorial Team
    Editorial Team added an answer Our team does something similar to this, so here's some… May 16, 2026 at 11:38 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.