2011-01-10 34 views
2

子コントロールを表示するために、ContentTemplateを持つカスタムコントロールがあります。データコンテキストがDataTemplateを通過していないので、子コントロールをバインドすると、その値を取得できません。私はDataTemplateに関してこれを正しく実装していないと確信していますので、何か助けていただければ幸いです。私はできるだけ小さなシナリオに問題を壊しました。カスタムコントロール内のDataContextの使用

まず、ページ:

<Window x:Class="MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="clr-namespace:WpfApplication1" 
Title="MainWindow" Height="350" Width="525"> 

<Window.Resources> 
    <ResourceDictionary> 
     <local:MainWindowViewModel x:Key="ViewModel" /> 
    </ResourceDictionary> 
</Window.Resources> 

<Grid DataContext="{StaticResource ViewModel}"> 
    <local:MyControl> 
     <local:MyControl.MainContent> 
      <DataTemplate> 
       <TextBlock Text="{Binding TextValue}" /> 
      </DataTemplate> 
     </local:MyControl.MainContent> 
    </local:MyControl> 
</Grid> 

次に、ViewModelに:

Public Class MainWindowViewModel 
    Implements INotifyPropertyChanged 

    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged 

    Private _textValue As String 
    Public Property TextValue() As String 
     Get 
      If String.IsNullOrEmpty(_textValue) Then 
       _textValue = "A default value" 
      End If 

      Return _textValue 
     End Get 
     Set(ByVal value As String) 
      _textValue = value 
      RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("TextValue")) 
     End Set 
    End Property 
End Class 

そして今、私のカスタム制御コードビハインド:

Public Class MyControl 
    Inherits System.Windows.Controls.Control 

    Shared Sub New() 
     DefaultStyleKeyProperty.OverrideMetadata(GetType(MyControl), New FrameworkPropertyMetadata(GetType(MyControl))) 
    End Sub 

    Public Property MainContent As DataTemplate 
     Get 
      Return GetValue(MainContentProperty) 
     End Get 

     Set(ByVal value As DataTemplate) 
      SetValue(MainContentProperty, value) 
     End Set 
    End Property 

    Public Shared ReadOnly MainContentProperty As DependencyProperty = _ 
          DependencyProperty.Register("MainContent", _ 
          GetType(DataTemplate), GetType(MyControl), _ 
          New FrameworkPropertyMetadata(Nothing)) 

End Class 

そして最後に、私のカスタムcont ROLの定義:これが実行されると

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="clr-namespace:WpfApplication1"> 


<Style TargetType="{x:Type local:MyControl}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:MyControl}"> 
       <StackPanel> 
        <TextBlock Text="Hello, World!" /> 
        <ContentControl x:Name="MainContentArea" ContentTemplate="{TemplateBinding MainContent}" /> 
       </StackPanel> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

、ViewModelに(TextValue)からの値がバインドされていません。

+1

ContentControlには点で少し特別であるとののDataContextにContentControlにのコンテンツをバインドしようとすることができ、あなたの問題

の気持ちを持っていますDataContextを返します。 DataTemplate内のDataContextは、DataContextではなくContentControlのコンテンツです。私はあなたのコードを試していませんでしたが、素早く見た後、私はあなたの問題である気持ちを持っています –

+0

@Meleak - あなたの答えをありがとう、私はDataContextが通過していないと感じていた。これを達成する別の方法をお勧めできますか?カスタムコントロール内の特定の場所に複数のコントロールをロードする必要があります。 –

+1

'ContentControl'の' Content'を '

答えて

5

ContentControlは、DataContextに関して少し特殊です。 DataTemplate内のDataContextは、そのDataContextではなく、ContentControlの内容です。私はあなたのコードを試してみませんでしたが、ざっと見た後、私はあなたが

<ContentControl x:Name="MainContentArea" Content="{Binding}" ... 
関連する問題