i have this DataTemplate:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Model="clr-namespace:Effectus.Model"
xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
>
<DataTemplate DataType="{x:Type Model:ToDoAction}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="Title"
Grid.Row="0" Grid.Column="0"/>
<TextBox Text="{Binding Path=Title}"
IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},Path=DataContext.AllowEditing.Value}"
Grid.Row="0" Grid.Column="1"/>
<TextBlock Text="Content"
Grid.Row="1" Grid.Column="0"/>
<TextBox Text="{Binding Path=Content}"
IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},Path=DataContext.AllowEditing.Value}"
AcceptsReturn="True"
MinHeight="100"
Grid.Row="2" Grid.ColumnSpan="2"/>
<TextBlock Text="Owner"
Grid.Row="6" Grid.Column="0"/>
<ComboBox SelectedItem="{Binding Path=Owner}" DisplayMemberPath="Username" SelectedValuePath="Username"
ItemsSource="{Binding Converter={StaticResource EnumValuesConverter}, ConverterParameter={x:Type Model:Status}}"
IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},Path=DataContext.AllowEditing.Value}"
Grid.Row="6" Grid.Column="1"/>
</Grid>
</DataTemplate>
In the last part you can see a TextBlock whose Text is “Owner”, followed by a ComboBox.
Just to give you the context, this is a little part of a ToDo app that i’m currently working on (i’m trying to get into MVVM).
The XAML i poted is the DataTemplate of the ToDoAction objects.
I want the “Owner” ComboBox to be filled by all users. I can get them from DB through NHibernate but i don’t have the faintest idea on how to bind the DataTemplate to my DataSource (a Nhibernate Session in my case, but i feel this is more general).
Can you give some little advise please?
Thank you all very much!
There are multiple ways to handle that like the ObjectDataProvider which i never used. But i think the most easiest and fastest is to use a singleton.
you can update and modify the Users list as usual and the bindings to this collection must be.