开发者

Using DataContext inside custom control

I have a custom control with a ContentTemplate to display child controls. The data context isn't passing through my DataTemplate and so when I bind my child control, I'm not able to retrieve that value. I'm pretty sure that I'm not implementing this correctly specifically with respect to the DataTemplate, so I would appreciate any help. I've broken the problem down into as small a scenario as I can.

First, the 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>

Next, the 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

And now my custom control code-behind:

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

And finally, my custom control definition:

<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>
                    <TextB开发者_Python百科lock Text="Hello, World!" />
                    <ContentControl x:Name="MainContentArea" ContentTemplate="{TemplateBinding MainContent}" />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

When this is run, the value from the ViewModel (TextValue) is not bound.


ContentControl is a little special in regards to DataContext. The DataContext within the DataTemplate is the Content of the ContentControl and not its DataContext. I didn't try your code but after a quick look I have a feelings that's your problem

You could try to bind the Content of the ContentControl to its DataContext with

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜