.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > How to bind UserControls to different sources in xaml
 

How to bind UserControls to different sources in xaml

I have a 2 user controls on the main page.
How to bind the controls to different sources and a ItemTemplate in xaml ?


<UserControl >
<ListBox x:Name="lbItems" Margin="0,0,0,20"
ItemTemplate="{StaticResource ItemTemplate1}"
ItemsSource="{Binding Path=Books, UpdateSourceTrigger=PropertyChanged}"
IsSynchronizedWithCurrentItem="True"
Visibility="Visible" SelectionMode="Single" >
</ListBox>
</UserControl>

<Window >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>

<cntrl:UCElementView x:Name="elmAuthors" Grid.Column="1" DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:Window1}}, Path=MyData, UpdateSourceTrigger=PropertyChanged }"/>

<cntrl:UCElementView x:Name="elmBooks" Grid.Column="0" DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:Window1}}, Path=MyData, UpdateSourceTrigger=PropertyChanged }"/>
</Grid>
</Window>


class MyData
{
public Collection<T>Books;
public Collection<T>Author;
}
Lucky7777
Try this:

<UserControl >
   <ListBox x:Name="lbItems" Margin="0,0,0,20"
                                    ItemTemplate="{StaticResource ItemTemplate1}"
                                    ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}"
                                    IsSynchronizedWithCurrentItem="True"
                                    Visibility="Visible" SelectionMode="Single" >
   </ListBox>
</UserControl>

<Window >
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <cntrl:UCElementView x:Name="elmAuthors" Grid.Column="1" DataContext="{Binding 
            RelativeSource={RelativeSource FindAncestor, 
            AncestorType={x:Type local:Window1}}, Path=MyData.Books, UpdateSourceTrigger=PropertyChanged }"/>

        <cntrl:UCElementView x:Name="elmBooks" Grid.Column="0" DataContext="{Binding 
            RelativeSource={RelativeSource FindAncestor, 
            AncestorType={x:Type local:Window1}}, Path=MyData.Author, UpdateSourceTrigger=PropertyChanged }"/>
    </Grid>
</Window>

Bigsby, Lisboa, Portugal - O que for, quando for, é que será o que é... http://bigsby.eu
  • Marked As Answer byLucky7777 Wednesday, September 23, 2009 3:49 PM
  • Unmarked As Answer byLucky7777 Wednesday, September 23, 2009 5:23 PM
  • Marked As Answer byLucky7777 Thursday, September 24, 2009 4:09 PM
  •  
Bigsby
There's better. You can define a DataTemplate for each data type using DataTemplate (suspense) DataType property.
One of the few examples where the documentation is crystal clear.
http://msdn.microsoft.com/en-us/library/system.windows.datatemplate.datatype.aspx

Bigsby, Lisboa, Portugal - O que for, quando for, é que será o que é... http://bigsby.eu
  • Marked As Answer byLucky7777 11 hours 35 minutes ago
  •  
Bigsby
Try this:

<UserControl >
   <ListBox x:Name="lbItems" Margin="0,0,0,20"
                                    ItemTemplate="{StaticResource ItemTemplate1}"
                                    ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}"
                                    IsSynchronizedWithCurrentItem="True"
                                    Visibility="Visible" SelectionMode="Single" >
   </ListBox>
</UserControl>

<Window >
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <cntrl:UCElementView x:Name="elmAuthors" Grid.Column="1" DataContext="{Binding 
            RelativeSource={RelativeSource FindAncestor, 
            AncestorType={x:Type local:Window1}}, Path=MyData.Books, UpdateSourceTrigger=PropertyChanged }"/>

        <cntrl:UCElementView x:Name="elmBooks" Grid.Column="0" DataContext="{Binding 
            RelativeSource={RelativeSource FindAncestor, 
            AncestorType={x:Type local:Window1}}, Path=MyData.Author, UpdateSourceTrigger=PropertyChanged }"/>
    </Grid>
</Window>

Bigsby, Lisboa, Portugal - O que for, quando for, é que será o que é... http://bigsby.eu
  • Marked As Answer byLucky7777 Wednesday, September 23, 2009 3:49 PM
  • Unmarked As Answer byLucky7777 Wednesday, September 23, 2009 5:23 PM
  • Marked As Answer byLucky7777 Thursday, September 24, 2009 4:09 PM
  •  
Bigsby
Thanks. It works.

Is there a way to define an item tempalte for the User Control's Listbox (such as use ItemTemplate1 or ItemTemplate2 )?
Lucky7777
There's better. You can define a DataTemplate for each data type using DataTemplate (suspense) DataType property.
One of the few examples where the documentation is crystal clear.
http://msdn.microsoft.com/en-us/library/system.windows.datatemplate.datatype.aspx

Bigsby, Lisboa, Portugal - O que for, quando for, é que será o que é... http://bigsby.eu
  • Marked As Answer byLucky7777 11 hours 35 minutes ago
  •  
Bigsby

You can use google to search for other answers

Custom Search

More Threads

• ScrollBar customization...
• FontCacheFullException stutters my application
• xaml.xsd
• Differences between ".../2006/xaml/presentation" and ".../2007/xaml/presentation" ???
• Trouble Adding Controls To WPF Forms
• How do I do Data Binding while limiting values?
• XBAP and https
• "Clearing" a binding
• WPF Binding and Adding records
• How to remove Next/Previous buttons in XAML