2009-10-21 5 views
0

J'ai un usercontrol que je veux utiliser en tant que DataTemplate dans une Listbox.Le contrôle de l'utilisateur n'est pas rendu lorsqu'il est utilisé en tant que DataTemplate?

Cela fonctionne:

<ListBox> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
<Grid x:Name="Grid" Height="100" Width="880" Background="LightGray"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="24"/> 
     <RowDefinition Height="24"/> 
     <RowDefinition Height="24"/> 
     <RowDefinition Height="24"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="190" /> 
     <ColumnDefinition Width="100" /> 
     <ColumnDefinition Width="100" /> 
     <ColumnDefinition Width="100" /> 
     <ColumnDefinition Width="190" /> 
     <ColumnDefinition Width="200" /> 
    </Grid.ColumnDefinitions> 
    <Label Grid.Column="0" Grid.Row="0">Client</Label> 
    <Label Grid.Column="0" Grid.Row="2">Contact</Label> 
    <Label Grid.Column="1" Grid.Row="0">Date Presentation</Label> 
    <Label Grid.Column="2" Grid.Row="0">Action</Label> 
    <Label Grid.Column="3" Grid.Row="0">Date Interview</Label> 
    <Label Grid.Column="3" Grid.Row="2">Time Interview</Label> 
    <Label Grid.Column="4" Grid.Row="0">Remarks</Label> 
    <Label Grid.Column="5" Margin="0,0,2,0">managed by</Label> 
    <ComboBox Grid.Column="0" Grid.Row="1" Margin="2" Text="{Binding Path=Customer}"> 
     <!--Template--> 
    </ComboBox> 
    <TextBox Grid.Column="0" Grid.Row="3" Margin="2" Text="{Binding Path=Contact}"></TextBox> 
    <TextBox Grid.Column="1" Grid.Row="1" Margin="2" Text="{Binding Path=PresentationDate}"></TextBox> 
    <ComboBox Grid.Column="2" Grid.Row="1" Margin="2" Text="{Binding Path=Action}"> 
     <!--Template--> 
    </ComboBox> 
    <TextBox Grid.Column="3" Grid.Row="1" Margin="2" Text="{Binding Path=InterviewDate}"></TextBox> 
    <TextBox Grid.Column="3" Grid.Row="3" Margin="2" Text="{Binding Path=InterviewTime}"></TextBox> 
    <TextBox Grid.Column="4" Grid.Row="1" Grid.RowSpan="3" Margin="2" Text="{Binding Path=Remarks}"></TextBox> 
    <StackPanel Orientation="Horizontal" Grid.Column="5" Grid.Row="1" > 
     <ComboBox Width="124" Text="{Binding Path=Manager}" Margin="2"></ComboBox> 
     <Button Width="60" Height="20" Margin="4,0,0,0" >Mail</Button> 
    </StackPanel> 
    <CheckBox Grid.Column="5" Grid.Row="3" Margin="2,2,4,2">Rejection communicated</CheckBox> 
    </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

Si je mets le même code d'entre les <DataTemplate> tags:

<UserControl x:Class="CandiMan.View.CandidatePresentationControl" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:cm="clr-namespace:CandiMan;assembly=CandiMan" 
    xmlns:vw="clr-namespace:CandiMan.View;assembly=CandiMan" 
    xmlns:vm="clr-namespace:CandiMan.ViewModel;assembly=CandiMan"    
    Height="100" Width="880" BorderBrush="Black" BorderThickness="1"> 

    <Grid x:Name="Grid" Height="100" Width="880" Background="LightGray"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="24"/> 
      <RowDefinition Height="24"/> 
      <RowDefinition Height="24"/> 
      <RowDefinition Height="24"/> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="190" /> 
      <ColumnDefinition Width="100" /> 
      <ColumnDefinition Width="100" /> 
      <ColumnDefinition Width="100" /> 
      <ColumnDefinition Width="190" /> 
      <ColumnDefinition Width="200" /> 
     </Grid.ColumnDefinitions> 
     <Label Grid.Column="0" Grid.Row="0">Client</Label> 
     <Label Grid.Column="0" Grid.Row="2">Contact</Label> 
     <Label Grid.Column="1" Grid.Row="0">Date Presentation</Label> 
     <Label Grid.Column="2" Grid.Row="0">Action</Label> 
     <Label Grid.Column="3" Grid.Row="0">Date Interview</Label> 
     <Label Grid.Column="3" Grid.Row="2">Time Interview</Label> 
     <Label Grid.Column="4" Grid.Row="0">Remarks</Label> 
     <Label Grid.Column="5" Margin="0,0,2,0">managed by</Label> 
     <ComboBox Grid.Column="0" Grid.Row="1" Margin="2" Text="{Binding Path=Customer}"> 
      <!--Template--> 
     </ComboBox> 
     <TextBox Grid.Column="0" Grid.Row="3" Margin="2" Text="{Binding Path=Contact}"></TextBox> 
     <TextBox Grid.Column="1" Grid.Row="1" Margin="2" Text="{Binding Path=PresentationDate}"></TextBox> 
     <ComboBox Grid.Column="2" Grid.Row="1" Margin="2" Text="{Binding Path=Action}"> 
      <!--Template--> 
     </ComboBox> 
     <TextBox Grid.Column="3" Grid.Row="1" Margin="2" Text="{Binding Path=InterviewDate}"></TextBox> 
     <TextBox Grid.Column="3" Grid.Row="3" Margin="2" Text="{Binding Path=InterviewTime}"></TextBox> 
     <TextBox Grid.Column="4" Grid.Row="1" Grid.RowSpan="3" Margin="2" Text="{Binding Path=Remarks}"></TextBox> 
     <StackPanel Orientation="Horizontal" Grid.Column="5" Grid.Row="1" > 
      <ComboBox Width="124" Text="{Binding Path=Manager}" Margin="2"></ComboBox> 
      <Button Width="60" Height="20" Margin="4,0,0,0" >Mail</Button> 
     </StackPanel> 
     <CheckBox Grid.Column="5" Grid.Row="3" Margin="2,2,4,2">Rejection communicated</CheckBox> 
     </Grid> 

</UserControl> 

dans un UserControl nommé CandidatePresentationControl et le faire comme

<ListBox> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
       <vw:CandidatePresentationControl/> 
      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

il n'est pas rendu. Pas d'erreurs, juste une liste vide. Est-ce que quelqu'un peut m'aider??

Merci!

edit: J'ai oublié quelque chose, je ne sais pas si c'est important: Le truc dans lequel je fais ça, c'est aussi un contrôle utilisateur.

+0

Où est votre contrôle utilisateur XAML? –

+0

J'ai un peu baissé l'exemple pour une meilleure lisibilité. C'est essentiellement la substance dans la boîte de code 2 ci-dessus, plus une grille et un peu plus de zones de texte et d'étiquettes. –

+0

Y at-il des informations dans la fenêtre de sortie de VS lorsque vous exécutez l'application en mode débogage? –

Répondre

1

Peu importe, votre UserControl référencé se trouve dans un autre UserControl. Essayez ces étapes pour mieux déboguer votre code XAML:

Puisque vous avez vos données câblées en XAML, la seule façon d'expliquer le ListBox vide est que votre UserControl ne peut pas être trouvé par le UserControl parent, imo .

+0

Merci pour ce lien! Je peux maintenant voir beaucoup plus de détails (et aussi quelques erreurs que je n'ai pas vues auparavant) –

+0

Eh bien, même avec cette sortie d'erreur étendue, il n'y a pas d'erreurs en utilisant le UserControl comme DataTemplate. = ( –

0
<ListBox> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
       <vw:CandidatePresentationControl DataContext="{Binding}"/> 
      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

Vous devez écrire cette façon afin de lier datacontext, je vous suggère de regarder MVVM qui vous donnera idée sur la façon de le faire de façon encore mieux.

+0

J'ai déjà lié la ListBox ItemsSource à une liste IEnumerable des PresentationViewModels que je veux dans cette listbox, à partir de ContactViewModel, à tout le UserControl qui est lié Dans un fichier ressources j'ai défini CandidatePresentationControl en tant que DataTemplate pour ce PresentationViewModel. –

Questions connexes