首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ListBox ItemTempate DataContext

ListBox ItemTempate DataContext
EN

Stack Overflow用户
提问于 2010-10-30 11:24:09
回答 2查看 1K关注 0票数 0

我有一个ListBox,它有它的ViewModel --让我们称之为ListBoxViewModel。列表框具有ItemsSource="{Binding MyItems}"属性,该属性对应于ListBoxViewModel中的ObservableCollection<MyItemType>。ListBox有它的ItemTemplate,它在ListBox中创建MyItemControl控件。问题是,我希望MyItemControl将MyItemType实例作为DataContext。但是DataContext是空的。什么是最好的实现?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-10-30 14:32:42

我的语法有点不对。正确的解决方案示例是:

App.xaml

代码语言:javascript
复制
<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

代码语言:javascript
复制
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

代码语言:javascript
复制
<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

代码语言:javascript
复制
namespace WpfProblem2 {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e) {
            listBox.DataContext = new ListBoxViewModel();
        }
    }
}

MyItemControl.xaml

代码语言:javascript
复制
<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

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2010-10-30 12:22:34

明确地设置数据上下文

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4058465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档