2010-12-07 5 views
1

목록 상자의 DataTemplate을 컨트롤의 내부 UserControl을 바인딩 :다음과 같이 I 사용자 컨트롤이 실버

public string CaseTitle 
    { 
     get { return (string)GetValue(TitleProperty); } 
     set { 
      SetValue(TitleProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for Title. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty TitleProperty = 
     DependencyProperty.Register("CaseTitle", typeof(string), typeof(SearchResultControl), new PropertyMetadata(new PropertyChangedCallback(SearchResultControl.OnValueChanged))); 

내 .xaml에서 : CaseTitle에 대한 종속성 속성을

<UserControl x:Class="CaseDatabase.Controls.SearchResultControl" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
d:DesignHeight="192" d:DesignWidth="433"> 

<Grid x:Name="LayoutRoot" Background="White" Height="230" Width="419"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="68*" /> 
     <RowDefinition Height="90*" /> 
    </Grid.RowDefinitions> 
    <TextBlock x:Name="TitleLink" Height="33" Text="{Binding CaseTitle}" HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top" Width="100" Foreground="Red"/> 
</Grid> 

을 페이지 목록 상자가 있고 그것의 데이터 형식 안에 내 컨트롤을 instanciate. 이 목록 상자의 ItemsSource는 도메인 서비스에 바인딩됩니다. 바인딩 작업을 알면 적절한 수의 elemets를 얻을 수 있지만 데이터가 전혀 표시되지 않습니다.

내 목록 상자에 대한 코드는 다음과 같다 :

<ListBox x:Name="SearchResultsList" Width="Auto" MinHeight="640" ItemsSource="{Binding ElementName=SearchDomainDataSource, Path=Data}" 
        Grid.Row="0" Grid.Column="0"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <Grid x:Name="LayoutRoot" Background="White" Height="158" Width="400"> 
        <my:SearchResultControl CaseTitle="{Binding Path=Title}" /> 
        </Grid> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

그래서 누군가가 내가 엉망으로하고 방법을 제안 할 수 있습니다 내 내 사용자 컨트롤에 바인딩? 감사합니다

답변

1

문제는 {Binding CaseTitle}이 (가) CaseTitle 종속성 속성을 찾지 못하는 것입니다. 바인딩이 사용하는 기본값 인 Source은 현재 바인딩 된 요소의 속성 인 DataContext의 현재 값입니다. 해당 개체는 UserControl이 아닙니다.

당신은 따라서 다음과 바인딩 같은 것을 변경해야 -

<TextBlock x:Name="TitleLink" Height="33" Text="{Binding Parent.CaseTitle, ElementName=LayoutRoot}" HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top" Width="100" Foreground="Red"/> 

지금 바인딩의 소스 객체 따라서, 그 ParentUserControl의 직접적인 자식 이름 "이 LayoutRoot"로 Grid된다 속성은 사용자 정의 컨트롤이므로 여기에서 CaseTitle 속성에 바인딩 할 수 있습니다.

+0

대단히 감사합니다. 완벽하게 작동합니다. – Andres