在自定义控件中使用DataContext

在自定义控件中使用DataContext

问题描述:

我有一个带有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> 

接下来,视图模型:

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 

最后,我的习俗续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> 

当这样运行时,从视图模型(TextValue)的值不被约束。

+1

ContentControl中是关于有点特别到DataContext。 DataTemplate中的DataContext是ContentControl的内容,而不是它的DataContext。我没有尝试你的代码,但经过一看,我有一种感觉,这是你的问题 – 2011-01-10 16:32:33

ContentControl对于DataContext有点特别。 DataTemplate中的DataContext是ContentControl的内容,而不是其DataContext。我没有尝试你的代码,但咋一看后,我有一个感受那是你的问题

你可以尝试到ContentControl中的内容结合到它的DataContext与

<ContentControl x:Name="MainContentArea" Content="{Binding}" ...