开发者

User Control Not Rendering When Used as a DataTemplate?

开发者 https://www.devze.com 2022-12-09 05:21 出处:网络
I have a usercontrol which I want to use as a DataTemplate in a Listbox. This works: <ListBox> <ListBox.ItemTemplate>

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

<L开发者_如何学PythonistBox>
    <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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消