2010-10-30 3 views
0

J'ai un ListBox qui a son ViewModel - appelons-le ListBoxViewModel. Zone de liste a l'attribut ItemsSource="{Binding MyItems}" qui correspond à ObservableCollection<MyItemType> dans ListBoxViewModel. ListBox a son ItemTemplate qui crée MyItemControl contrôles dans ListBox. La question est que je m'attends à ce que MyItemControl a MyItemType instance comme DataContext. Mais DataContext est null. Quelle est la meilleure implémentation?ListBox ItemTempate DataContext

Répondre

0

Je venais de mauvaise syntaxe quelque part. Exemple correct de la solution est:

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

Définir le contexte de données explictly