在自定义控件中使用DataContext

时间:2011-01-10 15:57:56

标签: wpf wpf-controls binding

我有一个带ContentTemplate的自定义控件来显示子控件。数据上下文没有通过我的DataTemplate,因此当我绑定我的子控件时,我无法检索该值。我很确定我没有特别针对DataTemplate实现这一点,所以我将不胜感激任何帮助。我已经把问题分解成尽可能小的情况。

首先,Page:

<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

最后,我的自定义控件定义:

<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 个答案:

答案 0 :(得分:5)

对于DataContext,

ContentControl有点特殊。 DataTemplate中的DataContext是ContentControl的内容,而不是其DataContext。我没有尝试你的代码,但经过快速浏览后,我感觉这是你的问题

您可以尝试使用

将ContentControl的内容绑定到其DataContext
<ContentControl x:Name="MainContentArea" Content="{Binding}" ...