2012-08-25 1 views
0

이 문제는 저를 괴롭 혔습니다. 누군가가 명백한 오류를 지적 할 수 있기를 바랍니다. 내 프로그램의 MainView에서 표에 추가 할 사용자 정의 컨트롤이 있습니다. 기본보기는 MainViewModel에 바인딩되고 usercontrol은 CardioVM에 바인딩됩니다.MVVM의 ComboBox가 표시되지 않음 List <string> items

테스트 라벨을 사용하여 사용자 정의 컨트롤의 라우팅이 올바른지 확인하고 모두 정상적으로 작동하는지 확인했습니다. 내 CardioVM에

List<string> CardioList 

Cardio.List<string> exercises 

에서

List<string> exercises 

내가 문자열을 통과하려고하는 속성이 무술라는 클래스가 있습니다.

List<string> CardioList 

을 디버깅 할 때

Cardio.List<string> exercises 

에서 항목으로 채워지고하지만 내 콤보는 화면에 표시되는 항목을 표시하지 않습니다. 여기 내 UserControl을위한 XAML하고 있습니다 :

<UserControl x:Class="CalendarTest.UserControl1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300" 
     DataContext="{Binding CardioVM, Source={StaticResource Locator}}"> 
<Grid> 
    <ComboBox ItemsSource="{Binding CardioList, Mode=OneWay}" SelectedItem="{Binding SelectedCardio, Mode=TwoWay}" Height="50"></ComboBox> 

</Grid> 

여기에 내 CardioVM에 대한 코드입니다 : 내가 여기 잘못 가고

public class CardioVM : ViewModelBase 
{ 
    public Cardio cardioItem { get; set; } 
    public CardioVM() 
    { 
     TestLabel = "Tester"; 
    } 

    //Test Label for binding testing 
    private string testLabel; 

    public string TestLabel 
    { 

     get { return testLabel; } 
     set 
     { 
      testLabel = value; 
      RaisePropertyChanged("TestLabel"); 
     } 
    } 

    public CardioVM(string Date, string File) 
    { 

     cardioItem = new Cardio(File, Date); 
     CardioList = new List<string>(cardioItem.exercises); 

    } 


    private List<string> cardioList; 

    public List<string> CardioList 
    { 

     get { return cardioList; } 
     set 
     { 
      cardioList = value; 
      RaisePropertyChanged("CardioList"); 
     } 
    } 

    private string _selectedCardio; 
    public string SelectedCardio 
    { 
     get { return _selectedCardio; } 
     set 
     { 
      _selectedCardio = value; 
      RaisePropertyChanged("SelectedCardio"); 
     } 

    } 

} 

}

확실하지 있지만 포인터가 많이 감사하게 될 것입니다. 내가 내 홈페이지보기 모델의 콘텐츠 컨트롤에 바인딩하게 proprty에 해당 UserControl에 추가하는 줄 알았는데 여기 어디에

은 다음과 같습니다

: 나는 나의 MainWindow를의 XAML에서 CardioVM에 대한 데이터 컨텍스트를 설정했다
public void NewTemplateExecute() 
    { 
     TextHideTab = "Close"; 
     NewTemplateType = ("New " + SelectedExercise + " Exercise Template"); 
     //Set the message and lists based on the exercise selected plus adds the drop down control 
     switch (SelectedExercise) 
     { 
      case "Cardio": 
       /// 
       //This is where I thought CardioVM was being added 
       /// 
       NewTemplateText = "Please choose a cardio exercise from the drop down list to the left. You can then select the duration of the exercise and the intensity. To add another exercise please press the plus button in the right hand corner"; 
       ExerciseDropDowns = new CardioVM(selectedDateLabel, @"Model\Repository\Local Data\CardioList.txt"); 

      break; 
      case "Weights": 
      NewTemplateText = "Please select a exercise type. you can refine your exercises by body area. Then add the number of sets and the reps per set. Add as many exercises as you like - dont forget to set to total duration"; 

      break; 

      case "HIIT": 
      NewTemplateText = "HIIT to add"; 
      break; 
     } 
     Messenger.Default.Send("NewTemplate"); 

    } 

<DataTemplate DataType="{x:Type local:CardioVM}"> 
    <view:UserControl1/> 
</DataTemplate> 

나는 내가 CaridoVM을 엮은했지만 나는 VM 로케이터를 통해 전송하지 않는 한이 데이터 바인딩을 얻을 수가 없었다 방식에 실수를 한 가정

+0

'DataContext = "{바인딩 CardioVM, 소스 = {StaticResource Locator}}" "기본값으로 ** 새로운 **'CardioVM'을 생성합니다 'TestLabel = "Tester"'만을 설정하는 컨스트럭터는 비어있을 것이다. 그리고 그것은 당신이'MainViewModel'에서 VM을 넘겨주지 않는다는 것을 의미합니다. 'MainView'와'MainViewModel'에도 게시하여 문제를 더 자세히 조사하도록하십시오. – nemesv

+0

좋아, 게시 할 것입니다 – Luthervd

+0

나는이 UserControl을 제거하고 Comobox를 Main으로 바인딩하여이 문제를 해결할 수있었습니다. – Luthervd

답변

1

nemesv에게 감사드립니다. 물론 당신은 자리를 잡았습니다. 내 CardioVM에서 DataContext을 제거했으며 이제 Cardio보기를 ViewModel에 바인드하기 위해 메인보기에 DataTemplate을 설정하면됩니다. 이제 Mainview에서 매개 변수가있는 cardioVM을 호출 할 수 있으며 예상대로 내 콤보 상자를 채 웁니다. MVVM의 기본 몇 가지를 만져 보려고 한 것 같습니다.