2009-10-21 28 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

相關問題