0

这让我很难过——希望有人能指出一个明显的错误。我有一个用户控件,我将其添加到程序 MainView 中的网格中。主视图绑定到 MainViewModel,用户控件绑定到 CardioVM。

我使用了一个测试标签来检查用户控件的路由是否正确并且一切正常。我有一个名为 Cardio 的类,它的属性为

    List<string> exercises 

我正在尝试传递字符串

Cardio.List<string> exercises 

到一个

List<string> CardioList 

在我的 CardioVM 中。调试时

List<string> CardioList 

正在填充来自

Cardio.List<string> exercises

但我的 ComboBox 没有在屏幕上显示项目。这是我的 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");
       }

   }

}

}

不知道我在这里哪里出错了,但任何指针都将不胜感激。

这是我认为我在主视图模型中将 userControl 添加到内容控件绑定属性的地方:

 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");

    }

我在 Mainwindow xaml 中将 CardioVM 的数据上下文设置为:

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

我想我在连接 CaridoVM 的方式上犯了一个错误,但除非我通过 VM 定位器发送它,否则似乎无法将它进行数据绑定

4

1 回答 1

1

感谢 nemesv - 你当然是正确的。DataContext从我的 CardioVM 中删除,现在只需在主视图中使用setDataTemplate将 Cardio 视图绑定到 ViewModel。我现在可以使用 Mainview 中的参数调用一个有氧运动虚拟机,它会按预期填充我的组合框。似乎我需要了解一些 MVVM 的基础知识

于 2012-08-26T18:52:21.620 回答