0
我有这样的DataTemplate:绑定的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>
在最后部分,你可以看到一个TextBlock其文本为“所有者”,接着是组合框。 只是为了给你的上下文,这是我目前正在处理的一个ToDo应用程序的一小部分(我试图进入MVVM)。 我所熟悉的XAML是ToDoAction对象的DataTemplate。 我想要“所有者”组合框由所有用户填充。我可以通过NHibernate从数据库中获取它们,但是我没有想到如何将DataTemplate绑定到我的DataSource(在我的例子中为Nhibernate Session,但我觉得这更一般)。 你能给点建议吗? 非常感谢大家!
这当然会起作用,但它不会打破MVVM背后的良好设计原则吗?我的意思是,我已经有了一个定义好的数据源(NHibernate的ISession)。添加一个像你所建议的静态会导致数据源翻倍。此外,当我添加一个新的用户,该列表将不会自动更新...我将不得不调用UserList.Instance()。添加(newUser);任何其他想法? – andreapier
理论上你是对的,它总是可以更好。但是,个人意义上我总是知道我没有创建自己的数据源(比如直接表指针,XML文件或者使用objectdataprovider)迟早会不够,我必须解决它。这就是为什么我优先考虑简单的解决方案,因为它更容易建立在他们之上,而不是围绕先前存在的解决方案工作。但我没有关于NHibernate的知识,所以也许这[link](http://www.codeproject.com/Articles/36109/The-WPF-NHibernate-Toolkit)更有用。 – dowhilefor
我接受了你的答案,因为它给了正确的方向。那么,按照你的意见,这并不完美,但也不坏。我认为这是一个很好的回报。谢谢! – andreapier