绑定到 db 表的 ComboBox 最初应为空 [英] ComboBox which is bound to a db table should initially be blank
问题描述
我有一个 WPF 组合框控件绑定到数据库表中的 edm 字段.这工作正常,只是它在控件启动时输入了不需要的第一个值.这是 xaml
I have a WPF combobox control bound to a edm field in a database table. This works fine except that it enters the first value in the control when it starts which is not wanted. Heres the xaml
<ComboBox x:Name="cbMeasure"
Width="104"
ItemsSource="{Binding Source={StaticResource ddMeasureViewSource}}"
DisplayMemberPath="Measure"
IsSynchronizedWithCurrentItem="True"
SelectedValuePath="Measure"
SelectedValue="{Binding Measure1}"/>
如果我只是对控件进行硬编码,它不会将第一个值放入变量中.这是那个 xaml 的样子
If I just hard code the control it doesn't put the first value in the variable. Heres what that xaml looks like
<ComboBox x:Name="cbMeasure" Width="104" Text="{Binding Measure1}">
<TextBlock Text="one"/>
<TextBlock Text="two"/>
<TextBlock Text="three"/>
<TextBlock Text="four"/>
</ComboBox>
我该怎么做才能使绑定数据库的组合框像文本框组合框那样以空值开头?这是一个问题,因为它将 SelectedValue 中的第一个值与变量 (Measure1) 绑定在一起.
What do I have to do to make the database bound combobox start with an empty value the way the textbox combobox does? This is a problem as it puts the first value in the SelectedValue bound to a variable (Measure1).
db 表 ddMeasure 看起来像:
The db table ddMeasure looks like:
RID Measure
--- -------
1 One
2 Two
3 Three
4 Four
因此将One"放入组合框选择中,并填充 Measure1 变量.
so "One" in put into the ComboBox selection and Measure1 variable is populated as well.
推荐答案
好的,我找到了解决方案.您必须将属性 IsSynchronizedWithCurrentItem 设置为 false,我以为我已经尝试过,但我想我只是删除了该属性,并且默认值必须为 true.所以 xaml 看起来像
OK I found the solution to this. You have to set the property IsSynchronizedWithCurrentItem to false which I thought I had tried but I think I just removed that property and the default must be true. So the xaml looks like
<ComboBox x:Name="cbMeasure"
Width="104"
ItemsSource="{Binding Source={StaticResource ddMeasureViewSource}}"
DisplayMemberPath="Measure"
IsSynchronizedWithCurrentItem="False"
SelectedValuePath="Measure"
SelectedValue="{Binding Measure1}"/>
希望这能帮助其他尝试查找此类绑定信息的人.
Hope this helps others who try to find info on this type of binding.
这篇关于绑定到 db 表的 ComboBox 最初应为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!