2009-10-21 50 views
0

我有一个用户控件,我想用作ListBox中的DataTemplate。当用作DataTemplate时用户控件不呈现?

这工作:

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

它不会被渲染。没有错误,只是一个空的列表框。有人能帮我吗??

谢谢!

编辑:我忘了一些东西,不知道是否重要:我正在做这件事的整个事情,也是一个用户控件。

+0

您的用户控件XAML在哪里? – 2009-10-21 15:24:25

+0

为了获得更好的可读性,我将这个例子稍微放了下来。它基本上是上面第二个代码框中的东西,外加一个网格和一些更多的文本框和标签。 – 2009-10-21 15:33:13

+0

在调试模式下运行应用程序时,VS的Output窗口中是否有任何信息? – 2009-10-22 06:22:42

回答

1

它应该没有关系,即您引用的UserControl在另一个UserControl内。尝试以下步骤以更好地调试您的XAML代码:http://beacosta.com/blog/?p=52

由于您的数据在XAML中硬连接,因此解释空ListBox的唯一方法是,您的UserControl无法通过父UserControl找到,imo 。

+0

感谢您的链接!我现在可以看到更多的细节(还有一些我从未见过的错误) – 2009-10-22 11:57:39

+0

那么,即使有了这个扩展的错误输出,使用UserControl作为DataTemplate也没有错误。 =( – 2009-10-22 12:50:27

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

你必须以绑定的DataContext来写这样一来,我建议你看一下MVVM,会给你如何做到这一点甚至更好的办法的想法。

+0

我已经将ListBox ItemsSource绑定到我想要的列表框中的PresentationViewModel的IEnumerable列表中,从ContactViewModel绑定到整个UserControl。在资源文件中,我将CandidatePresentationControl定义为此PresentationViewModel的DataTemplate。 – 2009-10-22 13:45:43

相关问题