저는 WPF MVVM을 사용하고 있습니다. 나는 특성 MenuHeader
와 MenuItems
와 MenuViewModel
을 가지고 XAML간단한 Wpf MVVM 바인딩 문제
<UserControl x:Class="Accounting.Menu"
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"
xmlns:local="clr-namespace:Accounting"
mc:Ignorable="d"
d:DesignHeight="105" d:DesignWidth="300">
<UserControl.DataContext>
<local:MenuViewModel/>
</UserControl.DataContext>
<StackPanel>
<StackPanel>
<TextBlock Text="{Binding Path=MenuHeader}"/>
</StackPanel>
<ListBox ItemsSource="{Binding Path=MenuItems}" Height="70"/>
</StackPanel>
</UserControl>
에 다음과 같은 코드를 작성했습니다. 런타임 중에 두 속성 모두에서 값을 가져옵니다. 이전 텍스트는 TextBlock
이고 뒤에는 ListBox
의 ItemSource에 바인딩됩니다. 그러나 솔루션을 실행하면 TextBlock 및 ListBox가 비어 있습니다.
편집 : 사전에 코드 뷰 모델의
public class MenuViewModel: ViewModelBase
{
AccountingDataClassesDataContext db;
private string _menuType;
public string MenuHeader { get; set; }
public ObservableCollection<string> MenuItems { get; set; }
public MenuViewModel()
{
}
public MenuViewModel(string menuType)
{
this._menuType = menuType;
db = new AccountingDataClassesDataContext();
if (menuType == "Vouchers")
{
var items = db.Vouchers.OrderBy(t => t.VoucherName).Select(v => v.VoucherName).ToList<string>();
if (items.Any())
{
MenuItems = new ObservableCollection<string>(items);
MenuHeader = "Vouchers";
}
}
else
{
System.Windows.MessageBox.Show("Menu not found");
}
}
}
감사합니다.
뷰 모델에'INotifyPropertyChanged'을 구현 했습니까? – ChrisF
ViewModel 코드 게시 –
뷰의 데이터 컨텍스트를 필수 뷰 모델로 설정 했습니까? –