2012-06-13 5 views
0

콤보 박스가 동일한 문자열 값을 가질 수있는 시나리오가 있습니다. 엑사 콤보 상자가 드롭 다운에서 값을 다음 수를 위해 : "테스트"나는 작성하고 "Test1을" "Test1을" "Test1을" "Test2를", 선택된 인덱스에 기초Wpf의 ComboBox가 SelectedIndex를 실행하지 않음

다른 콤보 박스.

<Grid > 
    <Grid.RowDefinitions> 
     <RowDefinition Height="40"></RowDefinition> 
    </Grid.RowDefinitions> 
    <ComboBox ItemsSource="{Binding Path=ComboList, Mode=OneWay}" 
       SelectedIndex="{Binding Path=ComboIndex, Mode=TwoWay}"/ > 
</Grid> 

뷰 모델은 다음과 같습니다 : 내가 직면하고

class TestViewModel : INotifyPropertyChanged 
{ 
    private IList<string> _comboList = new List<string> 
             { 
              "Test", 
              "Test1", 
              "Test1", 
              "Test1", 
              "Test2", 
             };  

    public IList<string> ComboList 
    { 
     get { return _comboList; } 
    } 


    private int _comboIndex; 

    public int ComboIndex 
    { 
     get { return _comboIndex; } 
     set 
     { 
      if (value == _comboIndex) 
      { 
       return; 
      } 

      _comboIndex = value; 
      OnPropertyChanged("ComboIndex"); 
     } 
    } 

    private void OnPropertyChanged(string prop) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(prop)); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

문제가 selectedIndex의는 "Test1을"에서 값을 변경 (같은 넣다 내가 같은 문자열 값 사이에 suffling하고 해고하지 않는다는 것입니다처럼 내 XAML은 같은데 2.

+0

왜 SelectedItem을 사용하지 않습니까? – blindmeis

+0

* 내가 직면 한 문제는 동일한 문자열 값 * 사이에 겪고있는 경우 SelectedIndex가 실행되지 않는다는 것입니다. 이 문장은 의미가 없습니다. 당신은 항상 다른 인덱스에 바인딩됩니다. – McGarnagle

+0

@blindmeis : selectedindex를 기반으로 다른 콤보 상자를 채우기 때문에 선택한 색인을 사용하고 있습니다. 아무리해도 SelectedItem 또는 selectedvalue도 descibed 시나리오에서 해고되지 않습니다. –

답변

0

대신 문자열을 캡슐화하는 List<string>에 결합 인덱스에서 본 인덱스 1에, 본은 "Test1을"예

public class Item 
{ 
    public Item(string v){ Value = v; } 
    public string Value{get; private set;} 
} 

List<Item>에 결합.

는 그런 나를 위해 일한 DisplayMemberPath

<ComboBox ItemsSource="{Binding Path=ComboList, Mode=OneWay}" 
      DisplayMemberPath="Value" 
      SelectedIndex="{Binding Path=ComboIndex, Mode=TwoWay}"/ > 

를 지정하기 위해 XAML을 수정. 나는 그런 관계를 필요로 할 때

1

내가 내 뷰 모델에서 관계를 만들고 단순히 뷰 모델 당신이 지금 MyItem의 목록을 작성하고 당신이 필요로하는처럼 채울 수있는이 컬렉션

public class MyItem 
{ 
    public string Name {get; set;}//your Test, Test1, Test1 ... 
    public List<string> Childs {get; set;} // the childs depending on the the Name 
} 

에 결합한다.

public List<MyItem> MyItemList {get;set;} 

xaml에서 이제는 관련 콤보 상자를 만들 수 있습니다.

<ComboBox ItemsSource="{Binding Path=MyItemList}" 
      SelectedItem="{Binding Path=ComboIndex, Mode=TwoWay}"/ > 

<ComboBox ItemsSource="{Binding Path=ComboIndex.Childs}" 
      SelectedItem="{Binding Path=MySelectedPropForChild, Mode=TwoWay}"/ > 

그래서 색인을 관리하지 않아도 관계를 구축 할 수 있습니다.

관련 문제