在自定义控件中使用DataContext

Mat*_*uwe 2 wpf binding wpf-controls

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

一,页面:

<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>
Run Code Online (Sandbox Code Playgroud)

接下来,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
Run Code Online (Sandbox Code Playgroud)

现在我的自定义控件代码隐藏:

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
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

运行此操作时,ViewModel(TextValue)中的值未绑定.

Fre*_*lad 5

ContentControl在DataContext方面有点特殊.其中的DataContext DataTemplateContentControl而不是其DataContext的内容.我没有尝试你的代码,但在快速浏览后,我有一种感觉,这是你的问题

您可以尝试将ContentControl的Content绑定到其DataContext

<ContentControl x:Name="MainContentArea" Content="{Binding}" ...
Run Code Online (Sandbox Code Playgroud)