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.