代码之家  ›  专栏  ›  技术社区  ›  Václav Dajbych

列表框项模板数据上下文

wpf
  •  0
  • Václav Dajbych  · 技术社区  · 15 年前

    列表框 列表框视图模型 . 列表框有 ItemsSource="{Binding MyItems}" ObservableCollection<MyItemType> 在里面 列表框视图模型 . 有它的 创造了 MyItemControl公司 控件 . 问题是我希望 实例为 数据上下文 . 但是 数据上下文 为空。什么是最好的实现?

    2 回复  |  直到 15 年前
        1
  •  0
  •   benPearce    15 年前

    明确设置数据上下文

        2
  •  0
  •   Václav Dajbych    15 年前

    我在某个地方语法不对。正确的解决方案示例是:

    <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; }
        }
    }
    

    主窗口.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>
    

    主窗口.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); }
            }
    
        }
    }