2010-12-07 4 views
1

の内部ユーザーコントロールをバインド私は、ユーザーコントロールを持っている:CaseTitleのための依存関係プロパティを持つ次のようにListBoxのDataTemplateを制御シルバー

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

を:私の.xamlで

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

私はリストボックスを持っていて、そのデータ型の中に私のコントロールをインスタンス化しています。このリストボックスのItemsSourceはドメインサービスにバインドされています。私は拘束力のある仕事を知っており、適切な数のエレメットを手に入れますが、データはまったく表示されません。

私のリストボックスのコードは以下の通りです:

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

だから、誰が、私は私が私のユーザーコントロールにバインドめちゃくちゃにしていますどのように提案することができますか? Thankx

答えて

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

UserControlの直接の子であるバインディング名でGridなり「LayoutRoot」のソースオブジェクトを、従ってそのParent - :あなたはそれゆえ、次のように結合することを変更する必要が

プロパティはユーザーコントロールで、そこからCaseTitleプロパティにバインドできます。

+0

ありがとうございます。それは完璧に働く – Andres

関連する問題