Wpf 将dataTemplate绑定到外部数据源

Wpf 将dataTemplate绑定到外部数据源,wpf,xaml,nhibernate,data-binding,Wpf,Xaml,Nhibernate,Data Binding,我有这个数据模板: <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.Contr

我有这个数据模板:

<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>

在最后一部分中,您可以看到文本为“Owner”的TextBlock,后跟一个组合框。 这只是我目前正在开发的ToDo应用程序的一小部分(我正在尝试进入MVVM)。 我创建的XAML是ToDoAction对象的数据模板。 我希望所有用户都填写“所有者”组合框。我可以通过NHibernate从DB获得它们,但我对如何将数据模板绑定到我的数据源(在我的例子中是一个NHibernate会话,但我觉得这更一般)一无所知。 你能给我一些建议吗?
非常感谢大家

有多种方法可以像我从未使用过的那样处理这个问题。但我认为最简单和最快的方法是使用单例

public sealed class UserList
{
    public ObservableCollection<User> Users {get; private set;}

    public static UserList Instance
    {
        get{return sInstance;}
    }

    private static UserList sInstance = new UserList();
}

这当然可行,但它不会打破MVVM背后的良好设计原则吗?我的意思是,我已经有了一个定义良好的数据源(NHibernate的ISession)。像您建议的那样添加一个静态的会导致数据源加倍。此外,当我添加新用户时,列表不会自动更新。。。我必须调用UserList.Instance().Add(newUser);还有其他想法吗?理论上你是对的,它总是可以更好。但就个人而言,我始终知道,我没有自己创建的数据源(如直接表指针、xml文件或使用objectdataprovider)迟早是不够的,我必须解决它。这就是为什么我更喜欢简单的解决方案,因为它更容易建立在它们之上,而不是围绕一个预先存在的解决方案工作。但我对NHibernate一无所知,所以这可能更有用。我接受了你的答案,因为它给出了正确的方向。嗯,不像你说的那么完美,但也不错。在我看来,这是一个很好的回报。谢谢
ItemsSource="{Binding Source={x:Static my:UserList.Instance}, Path=Users}