Reset DataContext on ComboBox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?V29ua28gdGhlIFNhbmU=?=

    Reset DataContext on ComboBox

    Hello,

    I have a ComboBox that has a DataContext set on it. When a PropertyChanged
    event is fired (based on a static instance of some class), I'd like to update
    the DataContext on the combo, but it seems that I need to set it to null
    first:

    Combo1.DataCont ext = null;
    Combo1.DataCont ext = dc;

    If I don't set it to null first, the combo's Items count is 0.

    Thanks,
    WtS
  • Ji Zhou [MSFT]

    #2
    RE: Reset DataContext on ComboBox

    Hello Paul,

    Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
    [MSFT] and I will be working on this issue with you.

    Based on my understanding, the current issue is that we use the data
    binding in WPF and update the comboBox's DataContext, but the data in the
    comboBox does not get updated unless we set the DataContext to null first
    and set it back. If any misunderstandin g here, please feel free to correct
    me.

    Actually, if we update the DataContext's content, the comboBox will not
    know it's DataContext has been changed. So, we will not see the combox
    items' updating. The way to work around this problem is to implement
    INotifyProperty Changed for the data in the DataContext. I write the
    following codes on my side which works fine:

    In the WPF xaml file:

    <Window x:Class="WpfApp lication1.Windo w1"
    xmlns="http://schemas.microso ft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microso ft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" Loaded="Window_ Loaded">
    <StackPanel>
    <ComboBox Name="comboBox1 " IsSynchronizedW ithCurrentItem= "True"

    ItemsSource="{B inding}" DisplayMemberPa th="CustomerNam e"/>
    <Button Name="button1" Click="button1_ Click">Button</Button>
    </StackPanel>
    </Window>

    I create a MyCustomer class which implements the INotifyProperty Changed
    interface:

    public class MyCustomer : INotifyProperty Changed
    {
    private string customerName = String.Empty;

    public event PropertyChanged EventHandler PropertyChanged ;

    private void NotifyPropertyC hanged(String info)
    {
    if (PropertyChange d != null)
    {
    PropertyChanged (this, new PropertyChanged EventArgs(info) );
    }
    }

    public MyCustomer(stri ng name)
    {
    this.customerNa me = name;
    }

    public string CustomerName
    {
    get { return this.customerNa me; }
    set
    {
    if (value != this.customerNa me)
    {
    this.customerNa me = value;
    NotifyPropertyC hanged("custome rName");
    }
    }
    }
    }

    In the Window1.xaml.cs , I have the following codes. Whenever the button is
    clicked, I update the dc's content and my combobox items get updated in the
    UI.

    public partial class Window1 : Window
    {
    ArrayList dc = new ArrayList();

    public Window1()
    {
    InitializeCompo nent();
    dc.Add(new MyCustomer("Ere ka"));
    dc.Add(new MyCustomer("Col bert"));
    }

    private void Window_Loaded(o bject sender, RoutedEventArgs e)
    {
    this.comboBox1. DataContext = dc;
    }

    private void button1_Click(o bject sender, RoutedEventArgs e)
    {
    (dc[0] as MyCustomer).Cus tomerName = "Tom";
    }
    }

    Please let me know if the above suggestion works for your scenario or not.
    And if you have any questions or concerns about this, please feel free to
    post.

    Best regards,
    Ji Zhou (v-jzho@online.mic rosoft.com, remove 'online.')
    Microsoft Online Community Support

    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    msdnmg@microsof t.com.

    =============== =============== =============== =====
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subs...#notifications.

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://support.microsoft.com/select/...tance&ln=en-us.
    =============== =============== =============== =====
    This posting is provided "AS IS" with no warranties, and confers no rights.

    Comment

    • =?Utf-8?B?V29ua28gdGhlIFNhbmU=?=

      #3
      RE: Reset DataContext on ComboBox

      Hi Zhou,

      Thank you - that essentially answers my question. However, I do see an
      issue if I take your example a little further. I added some buttons with
      event handlers:


      [Window1.xaml]
      <StackPanel>
      <ComboBox Name="comboBox1 " IsSynchronizedW ithCurrentItem= "True"

      ItemsSource="{B inding}" DisplayMemberPa th="CustomerNam e"/>

      <Button Name="button1" Click="button1_ Click">Change First
      Item</Button>
      <Button Name="button2" Click="button2_ Click">Add One Item</Button>
      <Button Name="button3" Click="button3_ Click">Add Items, No
      Null</Button>
      <Button Name="button4" Click="button4_ Click">Add Items, Null
      dc</Button>
      </StackPanel>

      [Window1.xaml.cs]
      private void button2_Click(o bject sender, RoutedEventArgs e)
      {
      dc.Add(new MyCustomer("Yin "));
      this.comboBox1. DataContext = dc;
      }

      private void button3_Click(o bject sender, RoutedEventArgs e)
      {
      dc.Add(new MyCustomer("Smo oshie"));
      dc.Add(new MyCustomer("Jac ko"));
      dc.Add(new MyCustomer("Fis her"));
      this.comboBox1. DataContext = dc;
      this.comboBox1. SelectedIndex = 3;
      }

      private void button4_Click(o bject sender, RoutedEventArgs e)
      {
      dc.Add(new MyCustomer("Ral ph"));
      dc.Add(new MyCustomer("Spe ncer"));
      dc.Add(new MyCustomer("Lul u"));
      this.comboBox1. DataContext = null;
      this.comboBox1. DataContext = dc;
      this.comboBox1. SelectedIndex = 4;
      }


      If I press button 2 to add a single customer, and then press button 3 to add
      other customers, I will see the customer as specified in the SelectedIndex.
      However, if you drop down the combo, only the original 3 (sometimes, only the
      original 2) items are shown. None of the new customers added in the button3
      handler are shown.

      If you run the application again, and this time choose button 4 instead of
      button 3, you will see all of the customers when expanding the combo. The
      only difference is that I set the DataContext to null before setting it to
      the updated ArrayList.

      Thanks,
      Paul

      Comment

      Working...