2009-10-21 2 views
0

ListBox에서 DataTemplate으로 사용하려는 usercontrol이 있습니다. 나는 <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> 

:

이 작동 CandidatePresentationControl라는 이름의 UserControl을에

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

<ListBox> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
       <vw:CandidatePresentationControl/> 
      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
처럼 할

렌더링되지 않습니다. 오류가없고 단지 빈 목록 상자. 누군가가 나를 도울 수 있습니까 ??

감사합니다.

편집 : 나는 뭔가를 잊어 버렸어. 중요하다면 몰라. 내가하고있는 모든 일은 usercontrol이기도하다.

+0

사용자 컨트롤 XAML은 어디에 있습니까? –

+0

가독성을 위해 예제를 약간 변경했습니다. 위의 2 번째 코드 상자에 기본적으로 포함 된 항목과 그리드 및 몇 가지 텍스트 상자 및 레이블. –

+0

디버그 모드에서 앱을 실행할 때 VS의 출력 창에 정보가 있습니까? –

답변

1

참조 된 UserControl이 다른 UserControl 내에 있다는 것은 중요하지 않습니다. http://beacosta.com/blog/?p=52

데이터가 XAML에 하드 연결되어 있으므로 비어있는 ListBox를 설명하는 유일한 방법은 부모 UserControl에서 UserControl을 찾을 수 없다는 것입니다. imo .

+0

그 링크를 가져 주셔서 감사합니다! 이제는 더 많은 세부 사항을 볼 수 있습니다. (이전에 보지 못했던 오류도 있습니다.) –

+0

이 확장 된 오류 출력이 있더라도 UserControl을 DataTemplate으로 사용하면 오류가 없습니다. = ( –

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

당신은 데이터 컨텍스트를 결합하기 위해이 방법을 쓸 필요가, 당신은 당신에게 더 나은 방법을 수행하는 방법에 대한 아이디어를 줄 것이다 MVVM 봐 제안.

+0

) ListBox ItemsSource를 이미 ContactViewModel에서 전체 ListBox에 바인딩 할 PresentationViewModels의 IEnumerable 목록에 바인딩했습니다. 리소스 파일에서이 PresentationViewModel에 대한 DataTemplate으로 CandidatePresentationControl을 정의했습니다. –