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


Top 5 Contributors of the Month
MarieAdela
Imran Ghani
Post New Web Links

CollectionViewSource/BindingListCollectionView to ObservableCollection/ListCollection

Posted By:      Posted Date: September 04, 2010    Points: 0   Category :WPF
 
i have watched the tutorial How Do I: Build a WPF Data Entry Form Using Entity Framework? i am confused why the need to change a CollectionViewSource/BindingListCollectionView to ObservableCollection/ListCollection start code:   var viewSource = (CollectionViewSource) this .Resources["customersViewSource" ]; var result = from cust in database.Customers select cust; viewSource.Source = result; view = (BindingListCollectionView)viewSource.View; to something like (end code )   var customerSource = ((CollectionViewSource)(this.FindResource("CustomerSource"))); var query = from c in db.Customers where c.City == "Seattle" select c; this.CustomerData = new CustomerCollection(query, db); customerSource.Source = this.CustomerData; this.View = ((ListCollectionView)(customerSource.View));   also in CustomerCollection protected override void InsertItem(int index, Customer cust) { this.context.AddToCustomers(cust); base.InsertItem(index, cust); } protected override void RemoveItem(int index) { this.context.DeleteObject(this[index]); base.RemoveItem(index); } then in here, why the need to insert/delete the customer from the context then from the base collection view? then why dont i need the same for update?  


View Complete Post


More Related Resource Links

CollectionViewSource sort not reflecting automatically after ObservableCollection item property chan

  

I use a CollectionViewSource to feed the Items of a ListView.  My CollectionViewSource is bound to an ObservableCollection that contains items that implement INotifyPropertyChanged.  Item property changes are being appropriately reflected in the ListView, but I'm surprised to find that the item does not automatically get resorted when its property that the sort is keying off of changes.  It would seem that all the information should be there for the ListView to recognize the need to resort the changed item.  Is there a way to make this happen or another 'lighter' alternative to calling Refresh() on the view?

 

Thanks!

 

mike


Advanced Basics: The ObservableCollection Class

  

Ken Getz shows how the CollectionChanged event lets you reflect changes to your underlying data source in your bound data controls.

Ken Getz

MSDN Magazine December 2008


Adding items to ObservableCollection via DataGrid

  
Hi All, I have a DataGrid bound to an ObservableCollection. I'd like to be able to add objects to the collection via the Grid. When I was working with sample data, I could go to the bottom of the grid, hit enter and add a new entry. Now that I'm using real data I don't seem to be able to do that. I tried taking a look at the class that the SampleData creates and can't quite fathom what it's up to. Does anyone know what I'm missing \ what I need to implement to make this possibe? Thanks Warrick

Creating an ObservableCollection out of TreeViewItems

  
Does anyone know how to create  an ObservableCollection out of TreeViewItems? My TreeViewItems follow a specific layout and I want that layout to be included in the TreeViewItem My TreeViewItems each contain 5 string values to make a TreeViewItem

CollectionViewSource Source = Linq to SQL

  
Hi, I am trying to bind data in my WPF Application using CollectionViewSource and LINQ to SQL. I have the following code in my XAML Where dbml:VendorsDataContext is my dbml file. In the code behind i have written the following code: addressViewSource.Source = bpo.GetVendorAddrTable(_vendorID); Where GetVendorAddrTable has the following code: var addr = from a in vendorsDC.tblVendor_Addresses where a.vendorID == _vendorID select new BussinessHelper.AddressContacts { VendorID = a.vendorID, Address1 = a.address1, Address2 = a.address2, City = a.tblVendor_City.cityName, State = a.tblVendor_State.stateName, Country = a.tblVendor_Country.countryName, VendorAddrID = a.vendorAddressID, CompleteAddr = a.address1 + a.address2 + a.tblVendor_City.cityName + a.tblVendor_State.stateName + a.tblVendor_Country.countryName                        };                  return addr;  When i run my application there is no data being bound. The CollectionViewSource source is null when i checked with code behind. Any ideas how to correct this?

What is the Equivalent of CollectionViewSource for only one Object?

  
I baiscally want to have access to the same things that a CollectionViewSource provides except that I am only dealing with one object. For example in CollectionViewSource I could do this: Source="{Binding Source={x:Static Application.Current}, Path=myObjects}" But there doesn't seem to be anythign else that has the "Source" option that only deals with a single object 

Refresh CollectionViewSource (Entity Framework) bound control

  
I can't believe I can't find an answer to this... I've created Entity Framework models generated from DB (SQL Server CE) I dragged an Entity from Data Sources to my MainWindow, automagically creating this XAML: <Window.Resources>     <CollectionViewSource x:Key="contentItemsViewSource" d:DesignSource="{d:DesignInstance my:ContentItem, CreateList=True}" /> </Window.Resources> <Grid DataContext="{StaticResource contentItemsViewSource}">     <DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="Auto" HorizontalAlignment="Stretch" ItemsSource="{Binding}" Margin="0,22" Name="contentItemsDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Stretch" Width="Auto" DataContext="{Binding}" IsReadOnly="True" CanUserResizeRows="False">         <DataGrid.Columns>             <DataGridTextColumn x:Name="idColumn" Binding="{Binding Path=Id}" Header="Id" Width="SizeToHeader" Visibility="Hidden" />             <DataGridTex

How to make ObservableCollection setter trigger when property value changes?

  

My class has a property ObservableCollection<MyType>. When any value of MyType changes, I want the setter for my property to be hit. It seems the setter is only hit when the CollectionChanged event is thrown, but in this case the Collection isn't changing, just a value of one of the properties in my Collection. 

 

Any pointers?


Entity Framework (EF) and ObservableCollection refresh

  

I'm currently so frustrated and can't seem to find a solution on google or msdn.

I'm using EF classes.  In my code, I have a ObservableCollection (CustomerCollection) which is assigned to a CollectionViewSource.  The data load/save fine.  The problem is when I delete one of the customers in the datacontext, the listview does not remove it.  Can someone tell me how to fix this?  Do I have to use the collectionview to delete/add all the times?

    private DataContext db = new DataContext();
    private CustomerCollection CustomerData;
    private ListCollectionView customerView;

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
      var query = from c in this.db.Customers
            where c.CompanyName.Contains(argSearchValue)
            || c.CustomerNo.Contains(argSearchValue)
            orderby c.CompanyName
            select c;

      var customerSource = ((CollectionViewSource)(this.FindResource("CustomerSource")));

      this.CustomerData = new CustomerCollection(query, this.db);
      customerSource.Source = this.CustomerData;
      this.customerView = ((ListCollectionView)(customerSource.View));

    } 

ObservableCollection to Datagrid in WPF

  

Hi All,

 

I'm newbie to programming. Now I just playing and learning.

I've made a WebService about this video tutorial: http://msdn.microsoft.com/en-us/netframework/first-steps-with-wcf.aspx

Everithing works fine. I've extended this with a little entity framework. Created a database and now my WPF client gets the data from my SQL database.

In my WPF clien I use DataGrid to show a few thing. I've heard I should use ObservableCollection as the itemsource and then for example if I submit a new record my list will refresh automatically and I get other cool things...

I need some help about this task. I don't really understand how could I implement this.

I'd like to show a few code snippet and ask somebody help me to rewrite my code to use ObservableCollection.

Well lets see the code snippets:

I generated my database from model definition. The generation created a class called EntityDB. EntityDB has a few properties like UserName, Password etc...

In my interface definition I have a methode defined:

[OperationContract]
List<EntityDB> GetPasswds();

The metho

Another newbie question about ObservableCollection and DataGrids

  

Hi All again,

 

I have a few questions and problems again... I found that if I want to search in a datagrid I have to define datagridview. Is it right?http://msdn.microsoft.com/en-us/library/ff407126.aspx

I have a generated class called EntityDB which properties are used in my datagrid and which porperties are get via webservice. In my previous topic I get help about how to use the ObservableCollection. If I want to define a datagridview in tha above link I found that I have to define my class as an inherited class from ObsevableCollection. As my class is generated I shouldn't modify it by hand to be an inherited class.

I think I should define a class at client side which will inherite from ObservableCollection and somehow in that class I should handle my original properties from EntityDB. What do you think about it? How would you do this?

An other question about the example in the msdn link above:

In that exam

Binding an ObservableCollection via XAML without code behind

  

 

I think I am missing something basic here.. But could someone show me what I am doing incorrectly?

MainWindow.xaml.cs

  public partial class MainWindow : Window
  {
    public ObservableCollection<string> TestOC = new ObservableCollection<string> { "First" };

    public MainWindow()
    {
      InitializeComponent();
      TestOC.Add("Second");     
    }

 the XAML file:

<Window x:Class="WpfApplication2.MainWindow"
    ...">
  <Grid>
    <ListView ItemsSource="{Binding TestOC}"/>
  </Grid>
</Window>

Shouldn't that just work?

 

ObservableCollection Binding in WPF

  

I'm new to WPF binding and I'm trying to read each line from a text file using a StringBuilder to manipulate each line, convert the result to a String, add the string to an ObservableCollection and display each line in a ListBox as it's added.  Everything is working except the dynamic update to the ListBox.  The ListBox displays the entire collection after the whole file is read but I'd like it to show each addition as it's added.

I expected that ObservableCollection's implementation of INotifyCollectionChanged would enable the binding to update the ListBox after each addtion, but that's not the case.  I've searched for answers but can't find an example that does just this.  Do I need to have a separate class derived from String that implements INotifyPropertyChanged?  If so, how might I do that?  Below is the binding I use:

Thanks for responding,

jphiggs

 

Xml Serialization: choose tag name for class instances in a generic ObservableCollection / List

  

Hi,

I've creating a class called Graphics, deriving from a generic ObservableCollection where T is a custom class called Graphic :

public class Graphics : ObservableCollection<Graphic>
{
public Graphics()
{
}
}

Another class has a Graphics field. When I serialize to XML this class, this works fine :

<MyOtherClassInstance>
 <Graphics>
  <Graphic id="1">
  <Graphic id="2">
  <Graphic id="3">
 </Graphics>
</MyOtherClassInstance>

But I would like to have the following:

<MyOtherClassInstance>
 <Gs>
  <Gr id="1">
  <Gr id="2">
  <Gr id="3">
 </Gs>
</MyOtherClassInstance>

How can I process, I can't find a solution.

Please help.

Thanks


Xml Serialization: change tag name of class instances in a generic ObservableCollection / List

  

Hi,

I've creating a class called Graphics, deriving from a generic ObservableCollection where T is a custom class called Graphic :

public class Graphics : ObservableCollection<Graphic>
{
public Graphics()
{
}
}

Another class has a Graphics field. When I serialize to XML this class, this works fine :

<MyOtherClassInstance>
 <Graphics>
  <Graphic id="1">
  <Graphic id="2">
  <Graphic id="3">
 </Graphics>
</MyOtherClassInstance>

But I would like to have the following:

<MyOtherClassInstance>
 <Gs>
  <Gr id="1">
  <Gr id="2">
  <Gr id="3">
 </Gs>
</MyOtherClassInstance>

How can I process, I can't find a solution.

Please help.

Thanks


Animate Shape inside a ListBoxItem Control Template bound to an ObservableCollection

  

Hi,

I basically have a listbox with a circular panel template, bound to an observablecollection "Hosts" defined like so:

<ListBox x:Name="listbox1" ItemContainerStyle="{DynamicResource ListBoxItemStyle2}" ItemsSource="{Binding Hosts}">
  <ListBox.Template>
  	<ControlTemplate TargetType="{x:Type ListBox}">
  		<WpfApplication7:CircularPanel AngleItem="{Binding Angle}" IsItemsHost="True"/>			
	</ControlTemplate> 		
  </ListBox.Template>
</ListBox>

 

Each ListBoxItem is also a rectangle, with the height and width of the rectangle bound to respective properties of a Host object (RecWidth and RecHeight):

 

<Style x:Key="ListBoxItemStyle2" TargetType="{x:Type ListBoxItem}">
	<Setter Property="Template">
		<Setter.Value

CollectionViewSource - problem filtering an ItemSource

  

I am trying to filter out duplicate items from a combo box. The items themselves are not duplicates, but I use a string value called "Name" to populate the combo box, and I only want one instance of that "Name" to show up.

Attached is a somewhat contrived example, but it shows the essence of what my problem is. I have a CollectionViewSource event handler called CollectionViewSource_Filter where I thought that I could get hold of the ItemsSource or CollectionViewSource and detect whether or not I have that string value already in the combo Box. If you compile and run the code, you see that all the items from the DataContext appear and duplicates are are not filtered out.

<Window x:Class="TestCombo.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">
  <StackPanel Orientation="Horizontal">
    <StackPanel.Resources>
Categories: 
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