Combobox不反映初始对象值
问题描述:
我有一个对象列表和一个网格中的几个字段。当选择在列表(lvInvoices
)的对象,我更新的网格(lyDetailForm
)的数据绑定:Combobox不反映初始对象值
private void lvInvoices_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int index = lvInvoices.SelectedIndex;
if (index != -1)
{
Invoice selectedInvoice = this.ListItems.ElementAt(index);
lyDetailForm.DataContext = selectedInvoice;
((PdfViewer)this.pdfControlHost.Child).File = selectedInvoice.SourceFile.FullName;
}
}
在lyDetailForms
,我有几个控制。当我设置网格的DataContext时,文本控件被正确更新。然而,组合框出现白色,直到我设置一次;之后,当我更改选定的项目时,它会被正确更新。
<Grid Name="lyDetailForm" Height="Auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="2" Grid.Row="1" Margin="10">
<TextBox Name="tbNif" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left" Width="100" Margin="5" IsEnabled="{Binding SpId, Converter={x:Static local:SpSentToBooleanConverter.Instance}, ConverterParameter=NEGATE, FallbackValue=False}" Text="{Binding Nif,ValidatesOnExceptions=True,NotifyOnValidationError=True}" Validation.Error="OnEditBoxError"/>
<ComboBox Name="cbType" Grid.Column="1" Grid.Row="2" Grid.RowSpan="2" Height="23" HorizontalAlignment="Left" Margin="5" VerticalAlignment="Top" Width="100" IsEnabled="{Binding SpId, Converter={x:Static local:SpSentToBooleanConverter.Instance}, ConverterParameter=NEGATE, FallbackValue=False}" Text="{Binding Type, Mode=TwoWay} ItemsSource="{Binding Types}"/>
</Grid>
顺便提一下,Types
是从同一invoice
对象返回字符串String[]
数组的静态属性。组合框项目是字符串。
有什么建议吗?提前致谢。
答
您使用的是cbType
组合框的文本框,这是错误的,去掉Text="{Binding Type, Mode=TwoWay}
绑定,并设置ComboBox中选定项的结合像这样SelectedItem={Binding SelectedType}
其中SelectedType
表示当前选择的类型。
我没有看到ComboBx绑定其SelectedItem任何地方。 – 2012-04-23 09:04:34
你是对的,我是绑定文本,而不是。然而,它一直工作,直到我开始设置对象的默认值。如果你把这个作为答案,我会授予你。 – SJuan76 2012-04-23 09:13:18
不,等待某人更好地描述测试/选定语义 – 2012-04-23 09:26:16