ComboBox 的 ItemTemplate 中的控件失去其绑定
我有一个使用 ItemTemplate 的 ComboBox,如下所示。不知何故,项目模板中定义的文本框的 Text 属性会与绑定断开连接,并在所选项目发生更改时停止更新。
ComboBox.ItemsSource 绑定到作为 CatheterDefinition 对象列表的 DependencyProperty。 ComboBox.SelectedItem 绑定到作为单个 CatheterDefinition 对象的 DependencyProperty。
<ComboBox
AutomationProperties.AutomationId="CatheterInfoModelFieldID"
VerticalAlignment="Center" HorizontalAlignment="Stretch"
ItemsSource="{x:Static PumpAndCatheter:CatheterInfoViewModel.CatheterModelDefinitions}"
SelectedItem="{Binding ElementName=UserControl, Path=ViewModel.SelectedCatheterModel, Mode=TwoWay, NotifyOnSourceUpdated=True}"
SourceUpdated="HandleModelSourceUpdated">
<ComboBox.ItemContainerStyle>
<!-- A style used to set the AutomationID based on the item goes here -->
</ComboBox.ItemContainerStyle>
<ComboBox.ItemTemplate>
<DataTemplate>
<!-- This line below is the location of the problem -->
<TextBlock Text="{Binding Converter={StaticResource CatheterModelDefinitionToStringConverter}}">
<!-- A style used to set the AutomationID based on the item goes here -->
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
我有一个自动化测试,它产生了一个非常奇怪的行为(我在代码的初始开发过程中多次看到相同的行为,但无法手动重现它) - 重现此行为的测试从 ComboBox 中选择一个项目,然后转到应用程序的另一部分并采取一些操作,最终将此更改保存在数据模型中。当测试返回到带有此 ComboBox 的屏幕时,它会尝试从 ComboBox 中选择另一个项目。 SelectedItem 发生变化,并且它必然会更改的值,但 ComboBox 中的文本不会更改 - 不知何故,与文本框的 Text 属性的绑定被破坏(或其他东西)...绑定仍然执行(当选择更改并转换为正确的值时,转换器仍然运行),但文本属性永远不会更新。
想法? (我无法提供这方面的示例,因为它是一个巨大的应用程序,并且只能在我知道的一项测试下重现)
I have a ComboBox that uses an ItemTemplate as shown below. Somehow the Text property of the text box defined in the item template gets disconnected from the binding and stops being updated when the selected item changes.
The ComboBox.ItemsSource is bound to a DependencyProperty that is list of CatheterDefinition objects. The ComboBox.SelectedItem is bound to a DependencyProperty that is a single CatheterDefinition object.
<ComboBox
AutomationProperties.AutomationId="CatheterInfoModelFieldID"
VerticalAlignment="Center" HorizontalAlignment="Stretch"
ItemsSource="{x:Static PumpAndCatheter:CatheterInfoViewModel.CatheterModelDefinitions}"
SelectedItem="{Binding ElementName=UserControl, Path=ViewModel.SelectedCatheterModel, Mode=TwoWay, NotifyOnSourceUpdated=True}"
SourceUpdated="HandleModelSourceUpdated">
<ComboBox.ItemContainerStyle>
<!-- A style used to set the AutomationID based on the item goes here -->
</ComboBox.ItemContainerStyle>
<ComboBox.ItemTemplate>
<DataTemplate>
<!-- This line below is the location of the problem -->
<TextBlock Text="{Binding Converter={StaticResource CatheterModelDefinitionToStringConverter}}">
<!-- A style used to set the AutomationID based on the item goes here -->
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
I have an automated test that produces a very strange behavior (I saw the same behavior a few time during the initial development of the code, but was unable to reproduce it manually) - The test that reproduces this selects an item form the ComboBox, then goes to another part of the application and takes some actions that end up saving this change in a data model. When the test returns to the screen with this ComboBox, it tries to select another item from the ComboBox. The SelectedItem changes, and the values that it is bound to change, BUT the text in the ComboBox does not change - somehow the binding to the Text property of the text box gets broken (or something)... The binding still executes (the converter still runs when the selection changes and it converts to the correct value), but the text property is never updated.
Thoughts? (I can't provide an example of this because it is a huge application and it is only reproducible under one test that I know of)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
绑定损坏大多数情况下是由于未调用(或未正确调用)OnPropertyChanged("PropName") 方法引起的。
如果没有看到您的底层实现,我会说这很可能是问题的根源。
Broken bindings are most of the time caused by not calling (or not correctly calling) the OnPropertyChanged("PropName") method.
Without seeing your underlying implementation, I would say that this is most likely the source of the problem.