473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.DataContext = null;
Combo1.DataContext = dc;

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

Thanks,
WtS
Sep 19 '08 #1
2 9775
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 misunderstanding 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
INotifyPropertyChanged 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="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<StackPanel>
<ComboBox Name="comboBox1" IsSynchronizedWithCurrentItem="True"

ItemsSource="{Binding}" DisplayMemberPath="CustomerName"/>
<Button Name="button1" Click="button1_Click">Button</Button>
</StackPanel>
</Window>

I create a MyCustomer class which implements the INotifyPropertyChanged
interface:

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

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}

public MyCustomer(string name)
{
this.customerName = name;
}

public string CustomerName
{
get { return this.customerName; }
set
{
if (value != this.customerName)
{
this.customerName = value;
NotifyPropertyChanged("customerName");
}
}
}
}

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()
{
InitializeComponent();
dc.Add(new MyCustomer("Ereka"));
dc.Add(new MyCustomer("Colbert"));
}

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

private void button1_Click(object sender, RoutedEventArgs e)
{
(dc[0] as MyCustomer).CustomerName = "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-****@online.microsoft.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:
ms****@microsoft.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.

Sep 22 '08 #2
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" IsSynchronizedWithCurrentItem="True"

ItemsSource="{Binding}" DisplayMemberPath="CustomerName"/>

<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(object sender, RoutedEventArgs e)
{
dc.Add(new MyCustomer("Yin"));
this.comboBox1.DataContext = dc;
}

private void button3_Click(object sender, RoutedEventArgs e)
{
dc.Add(new MyCustomer("Smooshie"));
dc.Add(new MyCustomer("Jacko"));
dc.Add(new MyCustomer("Fisher"));
this.comboBox1.DataContext = dc;
this.comboBox1.SelectedIndex = 3;
}

private void button4_Click(object sender, RoutedEventArgs e)
{
dc.Add(new MyCustomer("Ralph"));
dc.Add(new MyCustomer("Spencer"));
dc.Add(new MyCustomer("Lulu"));
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
Sep 22 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Matt | last post by:
I'm not entirely sure how to describe this issue. I have a number of ComboBoxes in my application which have their text properties bound to a field in a data set. The items loaded in the ComboBox...
5
by: dbspang | last post by:
I am using VBA and trying to find the right language to reset a combo box so it is blank. After the user selects an item in the combobox, there is a button that adds that item to a form. The...
1
by: =?Utf-8?B?VG9kZCBCZWF1bGlldQ==?= | last post by:
I have a "Save Changes" button on the form. I'm binding its IsEnabled property to a property of the object that I'm dealing with. When I connect the panel the buttons are in with an object (set the...
0
by: =?Utf-8?B?V29ua28gdGhlIFNhbmU=?= | last post by:
Hi All, Perhaps its because its been a long week, but I just can't seem to figure out how to bind data in a DataTemplate using DataContext. The following is in the XAML code: <Button...
5
by: Andy B | last post by:
I was just wondering, when you create dataContext methods, should you put business logic there to try and minimize pushing data through 2-3 layers of code? or should the business logic still go in...
0
by: Andy B | last post by:
I have a stored proc that selects all from a database table. I drug it from the server explorer onto the o/r designer to make it part of the dataContext. Now I can't figure out how to use it. I...
1
by: Ragesh Krishna | last post by:
Hello, I'm creating a WCF service in which I use LINQ to SQL to do all my data access. I have a couple of architectural questions about using LINQ to SQL from WCF services: 1) What is the...
1
by: Mike Gleason jr Couturier | last post by:
Hi, Is there any benefit to store a datacontext singleton in an application variable? Should I bother? Should I just create an instance of my datacontext every time my business objects...
2
by: =?Utf-8?B?UGFvbG8=?= | last post by:
How do I pass a DataContext to a button_click event handler. I have the following: public partial class frmMain : Form { public frmMain() { InitializeComponent(); string cnStr =
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.