Потребителският контрол не се изобразява, когато се използва като шаблон за данни?

Имам потребителски контрол, който искам да използвам като DataTemplate в Listbox.

Това работи:

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

Ако поставя абсолютно същия код между таговете <DataTemplate>:

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

в потребителски контрол с име CandidatePresentationControl и го направете като

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

не се изобразява. Няма грешки, само празен списък. Може ли някой да ми помогне??

Благодаря ти!

редактиране: Забравих нещо, не знам дали има значение: Всичко, в което правя това, също е потребителски контрол.


person Michael Niemand    schedule 21.10.2009    source източник
comment
Къде е вашият потребителски контрол XAML?   -  person Kent Boogaart    schedule 21.10.2009
comment
Намалих малко примера за по-добра четливост. Това са основно нещата във второто поле за код по-горе, плюс решетка и още няколко текстови полета и етикети.   -  person Michael Niemand    schedule 21.10.2009
comment
Има ли информация в изходния прозорец на VS, когато стартирате приложението в режим на отстраняване на грешки?   -  person Ben Childs    schedule 22.10.2009
comment
Няма грешки в данните в прозореца за изход, това вече е проверено.   -  person Michael Niemand    schedule 22.10.2009


Отговори (2)


Не би трябвало да има значение, че посоченият от вас потребителски контрол е в друг потребителски контрол. Опитайте тези стъпки за по-добро отстраняване на грешки във вашия XAML-код: http://beacosta.com/blog/?p=52

Тъй като вашите данни са твърдо свързани в XAML, единственият начин да обясните празния ListBox е, че вашият UserControl не може да бъде намерен от родителския UserControl, imo.

person Torsten    schedule 22.10.2009
comment
Благодаря за този линк! Сега мога да видя много повече подробности (а също и някои грешки, които не съм виждал преди) - person Michael Niemand; 22.10.2009
comment
Е, дори и с този разширен изход за грешка, няма грешки при използване на UserControl като DataTemplate. =( - person Michael Niemand; 22.10.2009

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

Трябва да пишете по този начин, за да обвържете datacontext, бих ви предложил да погледнете MVVM, който ще ви даде идея как да го направите още по-добре.

person Akash Kava    schedule 22.10.2009
comment
Вече обвързах ListBox ItemsSource към IEnumerable списък на PresentationViewModels, които искам в това списъчно поле, от ContactViewModel, към който е обвързан целият UserControl. Във файл с ресурси дефинирах CandidatePresentationControl като DataTemplate за този PresentationViewModel. - person Michael Niemand; 22.10.2009