views:

100

answers:

2

Hey folks,

I have a usercontrol which I want to use as a DataTemplate in a Listbox.

This works:

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

If I put the exact same code from between the <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>

into a usercontrol named CandidatePresentationControl and do it like

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

it does not get rendered. No errors, just an empty listbox. Can someone help me??

Thank you!

edit: I forgot something, dunno if it matters: The whole thing I'm doing this in, is a usercontrol, too.

+1  A: 

It shouldn't matter, that your referenced UserControl is within another UserControl. Try these steps to better debug your XAML-code: http://beacosta.com/blog/?p=52

Since you have your data hard wired in XAML, the only way to explain the empty ListBox is, that your UserControl can't be found by the parent UserControl, imo.

Torsten
Thanks for that link! I now can see a lot more detail (and also some errors that I haven't seen before)
MAD9
Well, even with this extended error output, there are no errors using the UserControl as DataTemplate. =(
MAD9
A: 
<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <vw:CandidatePresentationControl DataContext="{Binding}"/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

You have to write this way in order to bind datacontext, I would suggest you look at MVVM that will give you idea on how to do it even better way.

Akash Kava
I already bound the ListBox ItemsSource to an IEnumerable list of the PresentationViewModels that I want in that listbox, from the ContactViewModel, to that the whole UserControl is bound. In a resources file I defined the CandidatePresentationControl as a DataTemplate for this PresentationViewModel.
MAD9