Я не могу получить привязку текстового свойства для DataTemplate
в шаблоне проектирования MVVM.
Чтобы показать проблему, ниже приведено упрощение моей проблемы, в котором я связываю два разных свойства вида с одной моделью. свойство (aka AnObject.Text
).
Мой код в MainWindow.xaml
:
...
<Button Grid.Row="0" Content="{Binding ButtonText}" />
...
<ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto">
<ItemsControl ItemsSource="{Binding MyItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<DockPanel>
<Label Content="aaaaa" />
<TextBlock Text="{Binding ItemText}" />
</DockPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
...
Мой код в MainWindow.xaml.cs
(который устанавливает DataContext
для Button
и каждый элемент в <ItemsControl ItemsSource>
):
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
Мой код в MainWindowViewModel.cs
:
...
public ObservableCollection<object> MyItems => MyConverter.GetCollection(MyData.List);
public string ItemText => "dddd"; // this DOES works
public string ItemText => AnObject.Text; // this does NOT work
...
public string ButtonText => AnObject.Text; // this DOES works (note, same object property!)
...
Есть идеи, почему мое связывание внутри DataTemplate
не работает?
Заранее спасибо!