Silverlight WCF RIA:如何在 DataForms 中使用组合框
我在这个问题上花了近几个小时,我正在抓狂......我的代码如下:
<toolkit:DataForm MaxWidth="400" Name="dataForm_EditWBS" Header="WBS Task" AutoCommit="True" AutoEdit="True" AutoGeneratingField="dataForm_EditWBS_AutoGeneratingField">
<toolkit:DataForm.EditTemplate>
<DataTemplate>
<StackPanel>
<toolkit:DataField Label="Phase">
<ComboBox x:Name="ComboBoxEdit_Phase" DisplayMemberPath="PhaseDescription" SelectedValuePath="PhaseID"
SelectedItem="{Binding PhaseID, Mode=TwoWay}"
/>
</toolkit:DataField>
<toolkit:DataField Label="Task">
<TextBox
Text="{Binding TaskDescription, Mode=TwoWay}"/>
</toolkit:DataField>
<toolkit:DataField Label="Comments">
<TextBox HorizontalScrollBarVisibility="Auto" AcceptsReturn="True" Height="60" TextWrapping="Wrap"
Text="{Binding TaskComments, Mode=TwoWay}"/>
</toolkit:DataField>
<toolkit:DataField Label="Resource Type">
<ComboBox x:Name="cbResourceType"
SelectedItem="{Binding PhaseID, Mode=TwoWay}" DisplayMemberPath="PhaseDescription" SelectedValue="PhaseID" />
</toolkit:DataField>
<toolkit:DataField Label="Resource">
<ComboBox x:Name="cbResource"
SelectedItem="{Binding PhaseID, Mode=TwoWay}" DisplayMemberPath="PhaseDescription" SelectedValue="PhaseID" />
</toolkit:DataField>
<toolkit:DataField Label="Hours">
<TextBox
Text="{Binding Hours, Mode=TwoWay, StringFormat='n'}"/>
</toolkit:DataField>
<toolkit:DataField Label="Cost">
<TextBox
Text="{Binding Hours, Mode=TwoWay, StringFormat='c'}"/>
</toolkit:DataField>
</StackPanel>
</DataTemplate>
</toolkit:DataForm.EditTemplate>
</toolkit:DataForm>
</StackPanel>
我的组合框的来源来自另一个表,任何人都知道我如何用项目填充它。真的很感激!
谢谢,
尼古拉斯
I have spent nearly few hours on this issue and I am pulling my hair... My code follows:
<toolkit:DataForm MaxWidth="400" Name="dataForm_EditWBS" Header="WBS Task" AutoCommit="True" AutoEdit="True" AutoGeneratingField="dataForm_EditWBS_AutoGeneratingField">
<toolkit:DataForm.EditTemplate>
<DataTemplate>
<StackPanel>
<toolkit:DataField Label="Phase">
<ComboBox x:Name="ComboBoxEdit_Phase" DisplayMemberPath="PhaseDescription" SelectedValuePath="PhaseID"
SelectedItem="{Binding PhaseID, Mode=TwoWay}"
/>
</toolkit:DataField>
<toolkit:DataField Label="Task">
<TextBox
Text="{Binding TaskDescription, Mode=TwoWay}"/>
</toolkit:DataField>
<toolkit:DataField Label="Comments">
<TextBox HorizontalScrollBarVisibility="Auto" AcceptsReturn="True" Height="60" TextWrapping="Wrap"
Text="{Binding TaskComments, Mode=TwoWay}"/>
</toolkit:DataField>
<toolkit:DataField Label="Resource Type">
<ComboBox x:Name="cbResourceType"
SelectedItem="{Binding PhaseID, Mode=TwoWay}" DisplayMemberPath="PhaseDescription" SelectedValue="PhaseID" />
</toolkit:DataField>
<toolkit:DataField Label="Resource">
<ComboBox x:Name="cbResource"
SelectedItem="{Binding PhaseID, Mode=TwoWay}" DisplayMemberPath="PhaseDescription" SelectedValue="PhaseID" />
</toolkit:DataField>
<toolkit:DataField Label="Hours">
<TextBox
Text="{Binding Hours, Mode=TwoWay, StringFormat='n'}"/>
</toolkit:DataField>
<toolkit:DataField Label="Cost">
<TextBox
Text="{Binding Hours, Mode=TwoWay, StringFormat='c'}"/>
</toolkit:DataField>
</StackPanel>
</DataTemplate>
</toolkit:DataForm.EditTemplate>
</toolkit:DataForm>
</StackPanel>
The source for my comboboxes comes from another table, anyone know how I can populate this with items. Really appreciate it!
Thanks,
Nicholas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个类,对表单的所有数据绑定(包括组合框)所需的数据进行建模,并将组合框的 ItemsSource 数据绑定到它们需要显示的项目集合(作为新类的属性呈现)。然后将表单的 DataContext 设置为这个新类的实例。
一般来说,这个新类称为 ViewModel,这是模型/视图/ViewModel 设计模式 (MVVM) 的一个方面
Create a class that models the data that the form needs for all its data binding (including the comboboxes) and databind the ItemsSource of the comboboxes to the collection of items they need to display (present as a property on the new class). Then set the DataContext of the form to an instance of this new class.
Generally this new class is called a ViewModel and this is one aspect of the Model / View / ViewModel design pattern (MVVM)
继 Richards 的回答之后,请查看 Microsoft 的 Unity 和 Caliburn.Micro 以开始使用 MVVM。我尝试自己实现 MVVM,但这是一件可怕的事情。我提到的库让我们更容易理解 MVVM 以及如何实现它。
祝你好运。
Following on from Richards answer check out Microsoft's Unity and Caliburn.Micro to get started with MVVM. I tried to implement MVVM myselfand it was a horrid affair. The libraries I have mentioned make it much easier to understand MVVM and how to implement it.
Good luck.