我有一個列表框它有它的ViewModel - 我們稱之爲ListBoxViewModel。列表框中有ItemsSource="{Binding MyItems}"
屬性,對應於ObservableCollection<MyItemType>
的ListBoxViewModel。 列表框有其項目模板它創建MyItemControl控件在列表框。問題是,我期望MyItemControl具有MyItemType實例爲DataContext。但是DataContext爲空。什麼是最好的實施?ListBox ItemTempate DataContext
0
A
回答
0
我剛錯誤的語法地方。正確的解決方案的例子是:
的App.xaml
<Application x:Class="WpfProblem2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfProblem2"
StartupUri="MainWindow.xaml">
<Application.Resources>
<DataTemplate x:Key="myTemplate">
<local:MyItemControl/>
</DataTemplate>
</Application.Resources>
</Application>
ListBoxViewModel.cs
namespace WpfProblem2 {
public class ListBoxViewModel : DependencyObject {
public ListBoxViewModel() {
MyItems = new ObservableCollection<MyItemType>() {
new MyItemType() { TextValue = "A" },
new MyItemType() { TextValue = "B" },
new MyItemType() { TextValue = "C" },
new MyItemType() { TextValue = "D" }
};
}
public ObservableCollection<MyItemType> MyItems { get; private set; }
}
}
MainWindow.xaml
<Window x:Class="WpfProblem2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<ListBox x:Name="listBox" ItemTemplate="{StaticResource myTemplate}" ItemsSource="{Binding MyItems}" />
</Window>
個MainWindow.xaml.cs
namespace WpfProblem2 {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e) {
listBox.DataContext = new ListBoxViewModel();
}
}
}
MyItemControl.xaml
<UserControl x:Class="WpfProblem2.MyItemControl"
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="50" d:DesignWidth="300">
<TextBox Text="{Binding TextValue}" />
</UserControl>
MyItemType.cs
namespace WpfProblem2 {
public class MyItemType : DependencyObject {
public static readonly DependencyProperty TextValueProperty = DependencyProperty.Register("TextValue", typeof(string), typeof(MyItemType));
public string TextValue {
get { return (string)GetValue(TextValueProperty); }
set { SetValue(TextValueProperty, value); }
}
}
}
0
設置數據上下文顯式
相關問題
- 1. SelectedItem未知DataContext雖然設置了datacontext
- 2. DataControl上的UserControl的DataContext而不是WPF/MVVM中的ListBox DataBind
- 3. 如何在Silverlight中將dataContext的值賦給ListBox控件?
- 4. ListBox ItemsSource和DataContext未在Silverlight 3中綁定
- 5. 如何將數據從DataContext傳遞到WPF中的ListBox?
- 6. Chaining the ItemsSource DataContext
- 7. ContextMenu datacontext binding
- 8. 轉義WPF DataContext
- 9. 如何將ListBox元素從DataContext中選擇爲當ListBox綁定在代碼後面
- 10. Silverlight將DataContext傳遞給ConverterParameter?
- 11. 用於UserControl的DependencyProperty與DataContext
- 12. Datatemplate without Listbox/Itemssource
- 13. Silverlight ListBox DataTemplate
- 14. 爲什麼我的Listbox數據綁定使用DataContext不起作用?
- 15. 在WPF中,如何從包含ListBox的DataTemplate中將數據綁定到Window DataContext?
- 16. 當你給它一個ItemsSource時,在ListBox上設置DataContext的價值是什麼?
- 17. DataContext列表框中的ListPicker的DataContext問題
- 18. EventToCommand/DataContext
- 19. DataContext SubmitChanges
- 20. WPF如何設置DataContext
- 21. 從DataContext的C#通用表
- 22. 綁定SelectedItem更正WPF中的DataContext
- 23. 從listitemtemplate獲取頁面datacontext
- 24. 雙擊所選ListBox項目
- 25. DataContext db = new DataContext() - 作爲全局
- 26. 從另一個datacontext加載datacontext實體
- 27. 綁定到DataContext的子類
- 28. WPF ListBox拖放干擾ContextMenu?
- 29. 如何獲得家長的DataContext的
- 30. ComboBox在更改DataContext時觸發舊DataContext的觸發