아래 그림과 같이 ItemTemplate을 사용하는 ComboBox가 있습니다. 어떻게 든 항목 템플릿에 정의 된 텍스트 상자의 Text 속성은 바인딩에서 연결이 끊기고 선택한 항목이 변경되면 업데이트되지 않습니다.ComboBox의 ItemTemplate 컨트롤이 바인딩을 잃습니다.
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>
I가 매우 이상한 행동을 (내가 같은 동작을 코드의 초기 개발하는 동안 몇 시간을 보았지만, 수동으로 재현 할 수 없습니다) 생성하는 자동화 된 테스트 -이 선택을 재현 테스트를 항목이 ComboBox를 형성 한 다음 응용 프로그램의 다른 부분으로 이동하여 데이터 모델에서이 변경 사항을 저장하는 일부 작업을 수행합니다. 이 ComboBox가있는 화면으로 테스트가 돌아 오면 ComboBox에서 다른 항목을 선택하려고 시도합니다. SelectedItem이 변경되고 바인딩 할 값이 변경되지만 ComboBox의 텍스트는 변경되지 않습니다 - 텍스트 상자의 Text 속성에 대한 바인딩이 깨졌습니다 (또는 무언가) ... 바인딩이 여전히 실행됩니다 (변환기는 선택이 변경되고 올바른 값으로 변환 될 때 계속 실행되지만 텍스트 속성은 절대로 업데이트되지 않습니다.
생각하십니까? (이 예제는 거대한 응용 프로그램이므로 제공 할 수 없으며 알고있는 한 테스트에서 재생산 할 수 있습니다)
잘 모르겠지만 SelectedItem이 아니라 SelectedValue에 바인딩해야합니다. –