.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

DataContext scopes on bound ComboBox

Posted By:      Posted Date: October 15, 2010    Points: 0   Category :WPF


I'm just learning WPF and finding binding almost so easy that it's starting to confuse me. I've got a zip codes ComboBox inside of a Grid that has a DataContext set to a custom object. I set up a binding between my ComboBox's SelectedValue and my object's zip code ID. In my code behind I bind the ComboBox to an ADO.NET DataView containing the zip code ID, the zip code, and the city name. So I also set up a DataTemplate for my ComboBox items that will display the zip code and city. So here's my xaml:

  SelectedValuePath="ZipCodeID" Grid.Column=" 

View Complete Post

More Related Resource Links

ComboBox with bound SelectedItem displays new item even if selection fails internally



I'm a WPF beginner and currently exploring some techniques and use cases. This is one of my test setups:


<Window x:Class="wpfTest.MainWindow"
    Title="MainWindow" Height="350" Width

Bind An Already Bound TextBox to ComboBox SelectedItem


Hey all,


If I have a TextBox that is already two-way bound to something, but I also want it so when a ComboBox SelectedItem changes, then the TextBox's Text property is set to the new SelectedItem.


Here's what I have so far, I added the comment where the TextBox is, which I would like to also be bound to the ComboBox below this.


        <ListView Grid.Row="2" 
             DataContext="{Binding ScriptParameters}"

C# data bound combobox: display selected item text in separate label.content


I am new to C# but I'm resisting the urge to run screaming back to VB.NET. I'm working with a WPF application using C# code-behind and a lot of binding to a SQL 2005 database backend.

GOAL: I have a comboBox bound to a tbl_Systems entity which provides a list of systems for user selection. When the User selects a system from the comboBox I want to display the selection's text in a separate Label. The big picture is that several comboBox selections will be used to build a query string that will populate a listbox. So, I want to display these selected values in the Label so the user can see the query string they have dictated.

PROBLEM: When I set the Label content property to the comboBox SelectedItem property the entity tbl_System is displayed in the Label not the comboBox selection text.

SUSPICION: From reading similar examples in VB I suspect that the comboBox SelectedItem is actually a "datarowview" or the C# equivalent. But I don't know how to translate this explanation to C#.

The UI XAML comboBox properties are set as such:


Force update of bound combobox when validation changes bound source property


.NET 3.5SP1, VS 2008

I have inherited a system for maintenance and I'm working my way through defect reports.  Once such issue involves insuring a combobox value is reset to a known good value when a validation failure occurs.  The combobox is bound to a source property loaded which is initialized when the user selects a case for update.  According to this post it is an issue inside of WPF to ignore value changes when processing an update to the source (at least that's how I interpret the post).  Unfortunately for me, the UpdateSourceTrigger for a combobox is PropertyChanged, so the suggested approach does not fit my needs.  I tried it anyway, just to be certain.

I've created a test app which mimics the application design and exhibits the behavior I'm attempting to work around.  At this point, I'm at a loss how to proceed further.  I'm certain there's something I'm missing and would appreciate a nudge in the proper direction.

xaml window:

<Window x:Class="TestWpf.ComboTest

Combobox Set Selected Item from DataContext


i have a listview that houses a gridview .

I have the first column as combobox column defined in the datatemplated of the firstcolumn

I have set the Datacontext of the listview to a datatable having the folowing columns

Code,Name ,age.

i am able to set the binding for the 2 textboxes .

Here , the Code combobox has a set of allowed values that i am fetching from the Db as a datatable again .

now my requirement here is

1) to set the Itemsource of the combobox to the datatable. so that it has all the allowed codevalues.

2) the comboxbox for that particular row must show the (Selecteditem) corresponding value from the dataset of the Listview.

I am unable to set it itemsource directly because its in the datatemplate.

and i am unable to set binding to the selected item too.

Any help?

I tried setting the SelectedValue="{Binding Path=Code}" And its successfully getting the value.

But because i am unable to set the itemsource of it . i have written a Loaded event of the combobox and there i am settin the itemsource of it.

Hence Because of this. The selected value of the combobox is gettin overridden and is reset to null wheni am setting the itemsource.

i just need a way to set the itemsource first and then the binding to fire.


Compiler-generated scopes for local variable declarations

I was tasked with understanding and fixing a bug on error reporting with foreach iteration variables the other day, and it got me thinking about local variable scoping rules in C# in general. First, the bug.

Format Generic List Bound to Gridview


I have a generic list of a custom type. The custom type has a string and double.

I'm binding the list to a gridview and it outputs everything fine.

I'm trying to format the double column into a currency format. It is in a template field as I'm calculating a running total. I pass the value to a function that returns a double and adds the current value to a running total which I will show in the footer.

The format is not changing to currency as expected when using String.Format("{0:c}", GetTotal(Convert.ToDouble(Eval("Amount"))))

Any ideas on what might be going on and how to fix this?


How to take value of bound field of grid in a variable or in a textbox.


How to take value of bound field of grid in a variable or in a textbox.

 I m using this code in .aspx page for grid.

<asp:GridView ID="Grd_Mprocess" runat="server" AutoGenerateColumns="False"

                      Width="100%" AllowPaging="True"


                      AllowSorting="True" PageSize="5"  >

                 <RowStyle CssClass="odd" />


<asp:BoundField HeaderText="DOC_TYPE_PK" DataField="DOC_TYPE_PK"  Visible="true" >


  <asp:TemplateField HeaderText="DOC TYPE ID" Visible="true">  &nbs

DuplicateKeyException on datacontext.SubmitChanges()


I have a global variable for datacontext object, which I use for two different methods first for deleting a set of records and then inserting a set of records. The delete works fine but while insert, it throws the above exception when SubmitChanges is called.


Any help is appreciated........

Thanx in Advance. 

The type 'System.Data.Linq.DataContext' is defined in an assembly that is not referenced. You must



This is frustrating, and I don't know how to solve it.

I have a strange problem. I am adding a LinqDatasource object, and set the context:

        protected void LinqDataSource_ContextCreating(object sender, LinqDataSourceContextEventArgs e)
            e.ObjectInstance = new KaruselaDataContext(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

Then I get this error:
The type 'System.Data.Linq.DataContext' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.Linq, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.


On web.config I already have this:
<add assembly="System.Data.Linq, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

NOTE: The datacontext is in a different project (DLL proect) where I added a refernce to System.Data.Linq.

is there another way adding a refernce to a web project? or only though teh web.config?


How to display related tables in one crystal report and how to link this report with combobox?


Hi! I want to display a crystal report in my vb.net application. Suppose I have tables named student details, student marks, student address, etc... Now if I want to display all these details (fields of all tables) in one crystal report (with page breaks if necessary) then how will I achieve it. I will be providing a combo box in my application that contains list of student names. How can I link this combo box with the cystal report to dynamically display report for different student on selected index change of combo box? Help me friends. An example would be appreciable.

Data Points: Data Bound Applications with ADO.NET and Custom Objects


In this column see how to bind a custom list of business entities using the binding tools in the .NET Framework 2.0.

John Papa

MSDN Magazine February 2007

C++ at Work: Addin a Combobox Cancel Feature


This month Paul DiLascia codes some Microsoft Office-style dialog box features.

Paul DiLascia

MSDN Magazine August 2006

COM: Handle Late-bound Events within Visual Basic Using an ATL Bridge


Since a Visual Basic client doesn't handle events directly from late-bound COM objects, it needs some way to capture all the events and parameters launched by any COM object server instantiated at runtime and not known at design time. This article explains how to build a bridge component that does just that. The bridge component transmits the intercepted event data back to the Visual Basic client using another supporting COM object that is capable of holding event data and attributes. The Visual Basic client receives the notification from the bridge and extracts all the information relative to the event from the supporting object.

Carlo Randone

MSDN Magazine March 2001

ActiveX and Visual Basic: Enhance the Display of Long Text Strings in a Combobox or Listbox


The combobox and listbox controls in Visual Basic have no built-in support for displaying text strings that are too long to fit in the visible area of either control. This article describes several techniques to improve the readability of long strings in each control. The first technique uses tooltips to display a combobox or listbox item. The other techniques, which use various Windows APIs, include adding a horizontal scrollbar for a listbox and determining the necessary width of the longest string given the font attributes of the text. The required steps are described for both comboboxes and listboxes.

John Calvert

MSDN Magazine December 2000

asp expression bound to user control parameter?


How can I get pass the DataKey of a GridView to a usercontol, preferably without code behind.

Why doesn't this work:

<ucOrderDetails:AJAX_OrderDetail ID="od1" runat="server" OrderNumber='<%# GridView1.SelectedValue.ToString() %>'  />

GridView1 is just a list of orders with a DataKey of OrderNumber.

od1 is not inside of a databout control, does it need to be?

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend