這讓我難住 - 希望有人能指出一個明顯的錯誤。我有一個用戶控件,我將其添加到我的程序的MainView中的網格中。主視圖綁定到MainViewModel,而usercontrol綁定到CardioVM。MVVM中的組合框不顯示列表<string>項目
我已經使用測試標籤來檢查用戶控件的路由是否正確,並且所有工作都正常。我有一個名爲心類具有
List<string> exercises
我試圖通過字符串
Cardio.List<string> exercises
到
List<string> CardioList
在我CardioVM的屬性。當調試
List<string> CardioList
是越來越填充物品從
Cardio.List<string> exercises
,但我的組合框不顯示在屏幕上的項目。這裏是和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在我的主視圖模型:
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");
}
我曾創下CardioVM DataContext的在我的主窗口的XAML爲:
<DataTemplate DataType="{x:Type local:CardioVM}">
<view:UserControl1/>
</DataTemplate>
我相信我已經在我已經迷上了CaridoVM但也似乎沒有得到它的數據綁定,除非我把它通過VM定位的過程中出錯
通過設置DataContext =「{Binding CardioVM,Source = {StaticResource Locator}}」它會創建一個** new **'CardioVM',其默認值只設置TestLabel =「Tester」的構造器,所以你的列表將是空的。這意味着你不會從你的'MainViewModel'中傳遞虛擬機。發佈到你的'MainView'和'MainViewModel'讓我們進一步調查你的問題。 – nemesv
好吧會發布在 – Luthervd
以上我已經設法解決這個通過刪除usercontrol和綁定一個Comobox主要 – Luthervd