MVVM绑定到Windows Phone中的自定义控件

MVVM绑定到Windows Phone中的自定义控件,mvvm,windows-phone,Mvvm,Windows Phone,我为Windows Phone应用程序定义了以下简单的自定义控件。控件的XAML如下所示: <UserControl x:Class="PhoneApp1.MyControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com

我为Windows Phone应用程序定义了以下简单的自定义控件。控件的XAML如下所示:

<UserControl x:Class="PhoneApp1.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
         d:DesignHeight="480"
         d:DesignWidth="480"
         DataContext="{Binding RelativeSource={RelativeSource Self}}">

<Grid x:Name="LayoutRoot">
    <TextBlock Text="{Binding MyLabel}" />
</Grid>
现在的问题是,当我尝试将其数据绑定到我创建的视图模型时,什么都没有发生。各代码项如下所示:

public partial class MyControl : UserControl
{
    public MyControl()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty MyLabelProperty =
        DependencyProperty.Register("MyLabel", typeof (string), typeof (MyControl), new PropertyMetadata(default(string)));

    public string MyLabel
    {
        get { return (string) GetValue(MyLabelProperty); }
        set { SetValue(MyLabelProperty, value); }
    }
}
public class MyControlViewModel : ViewModelBase
{
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value;
        RaisePropertyChanged(() => Name);}
    }
}
在App.xaml中查看模型声明

<Application.Resources>
    <PhoneApp1:MyControlViewModel x:Key="MCVM" />
</Application.Resources>

控件的MainPage.xaml声明

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <PhoneApp1:MyControl  x:Name="testControl" DataContext="{StaticResource MCVM}" MyLabel="{Binding Path=MyLabel}" />            
    </Grid>

但是,如果我尝试将其数据绑定到其他UI元素,如下所示,它似乎可以工作吗

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <PhoneApp1:MyControl  x:Name="testControl" MyLabel="{Binding ElementName=Button, Path=Content}" />
        <Button Click="Button_Click" x:Name="Button" Content="Click" />
    </Grid>

我一直在努力让这个简单的场景工作,我认为我遗漏了一些愚蠢的东西

任何帮助都将不胜感激(这是WPF;我不确定这是否与WP7 silverlight 100%相似;我提供了进一步的详细信息,以帮助您了解发生了什么,以便在我的两个选项不可用时,您可以研究适当的解决方案)

默认情况下,绑定在xaml文件根元素(无论是窗口还是用户控件)的DataContext中生根。也就是说

<TextBlock Text="{Binding MyLabel}" />
这是很常见的,但这必须遵循规则,并且可能会导致一些性能问题。我发现做以下事情更容易,并建议:

<UserControl x:Class="PhoneApp1.MyControl"
             x:Name="root"
             x:SnipAttributesBecauseThisIsAnExample="true">
    <TextBlock Text="{Binding MyLabel, ElementName=root}" />
</UserControl>

为UserControl指定一个名称,然后使用ElementName重新设置绑定到可视树根的基础


我将省去其他更古怪的方法来实现这一点。

您想在文本块中显示MyLabel.Text还是什么?
<UserControl x:Class="PhoneApp1.MyControl"
             x:Name="root"
             x:SnipAttributesBecauseThisIsAnExample="true">
    <TextBlock Text="{Binding MyLabel, ElementName=root}" />
</UserControl>