2016-09-06 2 views
1

각 데이터 격자 행에 콤보 상자를 만들었습니다.wpf의 viewmodel에서 업데이트 할 때 콤보 상자의 SelectedItem 값이 비어집니다.

<ComboBox Width="166" 
      ItemTemplate="{StaticResource GridBinding}" 
      SelectedItem="{Binding Path=Car, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, IsAsync=True}" 
      SelectedValue="{Binding Path=Car, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, IsAsync=True}"> 
    <ComboBox.GroupStyle> 
     <GroupStyle HeaderTemplate="{StaticResource GroupHeader}" /> 
    </ComboBox.GroupStyle> 
    <ComboBox.Style> 
     <Style TargetType="ComboBox"> 
      <Setter Property="ItemsSource" Value="{Binding Path=Cars}" /> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType=DataGridCell}}" Value="True"> 
        <Setter Property="ItemsSource" Value="{Binding Path=DataContext.GroupedCars, RelativeSource={RelativeSource AncestorType=DataGrid}}" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </ComboBox.Style> 
</ComboBox> 

콤보 상자에서 "의 selectedItem을"결합하는 "자동차"속성이 "자동차"클래스의 목적은 ID, 이름, 등등과 같은 몇 가지 특성을 유지 : 다음 코드 조각은 콤보 상자를 만드는 데 사용됩니다

"Car"속성의 값을 업데이트하고 해당 Setter에서 "NotifyPropertyChanged"를 호출하면 콤보 상자의 "SelectedItem"값이 비어 있거나 비어 있다는 문제가 있습니다.

좋습니다.

+0

SelectedItem ** 또는 ** SelectedValue를 사용해야하며 둘 다 사용하면 안됩니다. – ibebbs

+0

"SelectedItem"만 사용하면 viewmodel에서 변경할 때 UI의 값을 업데이트하지 않습니다. 이는 "SelectedItem"이 참조를 사용하여 itemsource의 각 항목과 값을 비교하기 때문입니다. –

답변

0

SelectedItem은 더 이상 Collection에서 찾을 수 없으며 (ItemSource를 업데이트 할 때) null로 설정됩니다.

난 당신이 업데이트 할 때

<ComboBox ItemsSource="{Binding Cars}" 
      SelectedItem="{Binding Car}"> 
    <ComboBox.Style> 
     <Style TargetType="ComboBox"> 
      <Style.Triggers> 
       <Trigger Property="SelectedItem" Value="{x:Null}"> 
        <Setter Property="SelectedIndex" Value="0" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </ComboBox.Style> 
</ComboBox> 

가 이제 첫 번째 항목을 선택 얻을 것이다 증명하기 위해 XAML을 단순화했습니다.

+0

도움 주셔서 감사합니다. ibebbs가 제안한 위의 접근 방식은 [https://rachel53461.wordpress.com/2011/08/20/comboboxs-selecteditem-not-displaying/] 링크를 사용하여 나를 위해 일했습니다. –

관련 문제