Click here to Skip to main content
15,894,825 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: Data binding from a List to a Grid Pin
mittalamit24-Jun-09 20:18
mittalamit24-Jun-09 20:18 
GeneralRe: Data binding from a List to a Grid (unfortunately, it did not work) [modified] Pin
fjparisIII25-Jun-09 4:56
fjparisIII25-Jun-09 4:56 
AnswerRe: Data binding from a List to a Grid Pin
Mark Salsbery25-Jun-09 6:58
Mark Salsbery25-Jun-09 6:58 
GeneralRe: Data binding from a List to a Grid Pin
fjparisIII25-Jun-09 7:31
fjparisIII25-Jun-09 7:31 
GeneralRe: Data binding from a List to a Grid Pin
Mark Salsbery25-Jun-09 7:48
Mark Salsbery25-Jun-09 7:48 
GeneralRe: Data binding from a List to a Grid Pin
fjparisIII25-Jun-09 8:30
fjparisIII25-Jun-09 8:30 
GeneralRe: Data binding from a List to a Grid Pin
fjparisIII25-Jun-09 7:39
fjparisIII25-Jun-09 7:39 
GeneralRe: Data binding from a List to a Grid Pin
Pete O'Hanlon25-Jun-09 9:07
mvePete O'Hanlon25-Jun-09 9:07 
Sorry to wade into your discussion at this late stage, but there are a couple of things you should be thinking about. First of all, when you are working with collections of data in WPF it's generally a good idea to use an ObservableCollection rather than a list. Basically ObservableCollection exposes some handy events that WPF uses to add and remove items in the view. If you use this with items that implement INotifyPropertyChanged properly, you have a very powerful binding mechanism that requires minimal coding on your end to keep up to date.

Now, you can declare your DataContext very easily on an individual window. In my Twitter[^] sample, I do this in all of my windows - there's no code behind setting the context, and each window has its own data source. Here's a sample of how to do it in XAML:
<UserControl
  x:Class="SongBird.FriendsView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Width="420"
  Height="380"
  xmlns:vm="clr-namespace:SongBird.ViewModel"
  xmlns:twitter="clr-namespace:SongBird.Twitter"
  >
  <UserControl.Resources>
    <ObjectDataProvider x:Key="VM" ObjectType="{x:Type vm:PostViewModel}">
      <ObjectDataProvider.ConstructorParameters>
        <twitter:StatusType>Friends</twitter:StatusType>
      </ObjectDataProvider.ConstructorParameters>
    </ObjectDataProvider>
  </UserControl.Resources>

  <Grid DataContext="{Binding Source={StaticResource VM}}" >
    <ListBox
      Height="380"
      ItemsSource="{Binding Mode=TwoWay, Path=Model}"
      ItemTemplate="{DynamicResource postItem}"
      Padding="3"
      ScrollViewer.VerticalScrollBarVisibility="Auto"/>
  </Grid>
</UserControl>
Here's a breakdown of how this code works. In UserControl.Resources, I create and ObjectDataProvider that instantiates a class called PostViewModel. This class has a constructor with an enumerated parameter, so we set this up in the ObjectDataProvider.ConstructorParameters portion. Now, as you can see, we've called this ObjectDataProvider VM. This enables us to set the DataContext in the Grid, by binding it to the resource called VM. Have a download of Songbird[^], and see how this hands together.

"WPF has many lovers. It's a veritable porn star!" - Josh Smith

As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.


My blog | My articles | MoXAML PowerToys | Onyx



GeneralRe: Data binding from a List to a Grid Pin
fjparisIII25-Jun-09 9:29
fjparisIII25-Jun-09 9:29 
AnswerRe: Data binding from a List to a Grid Pin
Mark Salsbery25-Jun-09 7:23
Mark Salsbery25-Jun-09 7:23 
GeneralRe: Data binding from a List to a Grid Pin
fjparisIII25-Jun-09 7:40
fjparisIII25-Jun-09 7:40 
GeneralRe: Data binding from a List to a Grid Pin
Mark Salsbery25-Jun-09 7:46
Mark Salsbery25-Jun-09 7:46 
GeneralRe: Data binding from a List to a Grid Pin
fjparisIII25-Jun-09 8:33
fjparisIII25-Jun-09 8:33 
AnswerRe: Data binding from a List to a Grid (Everything works now) [modified] Pin
fjparisIII25-Jun-09 12:19
fjparisIII25-Jun-09 12:19 
QuestionWPF Slider project Pin
fgimenez24-Jun-09 8:14
fgimenez24-Jun-09 8:14 
AnswerRe: WPF Slider project Pin
Christian Graus24-Jun-09 15:26
protectorChristian Graus24-Jun-09 15:26 
GeneralRe: WPF Slider project Pin
fgimenez29-Jun-09 2:49
fgimenez29-Jun-09 2:49 
QuestionSilverlight2Chat Pin
deep7624-Jun-09 4:59
deep7624-Jun-09 4:59 
AnswerRe: Silverlight2Chat Pin
Mark Salsbery24-Jun-09 7:35
Mark Salsbery24-Jun-09 7:35 
GeneralRe: Silverlight2Chat Pin
deep7624-Jun-09 7:41
deep7624-Jun-09 7:41 
GeneralRe: Silverlight2Chat Pin
Mark Salsbery24-Jun-09 7:49
Mark Salsbery24-Jun-09 7:49 
GeneralRe: Silverlight2Chat Pin
deep7624-Jun-09 8:04
deep7624-Jun-09 8:04 
GeneralRe: Silverlight2Chat Pin
Mark Salsbery24-Jun-09 8:25
Mark Salsbery24-Jun-09 8:25 
GeneralRe: Silverlight2Chat Pin
deep7624-Jun-09 8:37
deep7624-Jun-09 8:37 
GeneralRe: Silverlight2Chat Pin
Mark Salsbery24-Jun-09 9:00
Mark Salsbery24-Jun-09 9:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.