gpt4 book ai didi

C# Silverlight - 具有双向绑定(bind)的组合框?

转载 作者:行者123 更新时间:2023-11-30 22:48:49 24 4
gpt4 key购买 nike

编辑:

我一直在寻找的答案是......

<dataFormToolkit:DataField Label="Business Type:">
<ComboBox x:Name="BusinessType" SelectedItem="{Binding BusinessType, Mode=TwoWay}" >
<ComboBox.Items>
<sys:String>Land</sys:String>
<sys:String>Maritime</sys:String>
</ComboBox.Items>
</ComboBox>
</dataFormToolkit:DataField>

这是 article 的链接

我有一个 c# silverlight 业务应用程序,它使用 ado.net Entity Framework 和域服务类来绑定(bind)到我的 sql server 数据库,并从我的数据库中提取数据/将数据保存到我的数据库中。我一直在使用 dataformtoolkit 命名空间来布局可以编辑/显示数据的文本框,使用双向绑定(bind)模式以允许读/写功能。

在某些字段上,我想使用组合框而不是文本框,以便为我的应用程序添加更好的用户体验。我从网上阅读得到的印象是,这并不像它应该的那样容易。我当前的文本框代码如下所示:

<dataFormToolkit:DataField>
<TextBox Text="{Binding BusinessType, Mode=TwoWay}" />
</dataFormToolkit:DataField>

我的类似尝试如下但不正确:

<ComboBox>
<ComboBox.Items>
<ComboBoxItem Content="Maritime" IsSelected="{Binding BusinessType, Mode=TwoWay}" />
<ComboBoxItem Content="Land" IsSelected="{Binding BusinessType, Mode=TwoWay}" />
</ComboBox.Items>
</ComboBox>

NB/我希望组合框由列表或枚举等(最好是列表)填充。组合框的内容应该与数据库无关,只是当用户点击提交时,组合框中所做的选择会持久保存回数据库。如果是这种情况,组合框可以从数据库中读取并显示已经做出的特定选择也很重要。

****编辑:

绑定(bind)到数据网格的数据表单的当前设置,其中可编辑的业务类型字段作为文本框(我想用具有两个可选项目的组合框替换此文本框)。

 <!--DataForm Declaration-->
<dataFormToolkit:DataForm x:Name="dataForm1" Height="410" Width="331"
VerticalAlignment="Top"
Header="Job Details"
CurrentItem="{Binding SelectedItem, ElementName=dataGrid1}"
HorizontalAlignment="Left" >
<dataFormToolkit:DataForm.EditTemplate>
<DataTemplate>
<StackPanel>
<dataFormToolkit:DataField>
<TextBox Text="{Binding BusinessType, Mode=TwoWay}" />
</dataFormToolkit:DataField>
</StackPanel>
</DataTemplate>
</dataFormToolkit:DataForm.EditTemplate>
</dataFormToolkit:DataForm>

那么我该如何操作这段代码来使用组合框而不是文本框呢?

如有任何帮助,我们将不胜感激。

最佳答案

您应该将绑定(bind)设置为使用 ComboBox 的 SelectedValue 属性。

<ComboBox SelectedValue="{Binding BusinessType, Mode=TwoWay}">...</ComboBox>

问题在于 ListBox 和 ComboBox 将对 SelectedItem 中的对象使用 Equals() 方法,因此如果类型不匹配,则 ComboBox 将不会将适当的项目设置为选中状态。因此,BusinessType 需要是一个字符串,因为您正在使用 ComboBoxItem 并指定字符串内容。

如果绑定(bind) ComboBox 的 ItemsSource,那么您将使用 SelectedItem,它实际上也是一个实体类型,在这种情况下,您可以更灵活地控制什么等于什么。

关于C# Silverlight - 具有双向绑定(bind)的组合框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1461167/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com