这让我很难过 - 希望有人可以指出一个明显的错误。我有一个用户控件,我将添加到我的程序的MainView中的网格。主视图绑定到MainViewModel,usercontrol绑定到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中添加到主视图模型中的内容控件绑定proprty的位置:
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");
}
我在主窗口xaml中为CardioVM设置了datacontext:
<DataTemplate DataType="{x:Type local:CardioVM}">
<view:UserControl1/>
</DataTemplate>
我认为我在连接CaridoVM的方式上犯了一个错误,但除非我通过VM定位器发送它,否则似乎无法将其发送到数据绑定
答案 0 :(得分:1)
感谢nemesv - 你当然是当之无愧的。从我的CardioVM中删除DataContext
,现在只需使用主视图中设置的DataTemplate
将Cardio视图绑定到ViewModel。我现在可以使用Mainview中的参数调用cardioVM,并按预期填充我的组合框。似乎我不想修改一些MVVM的基础知识