繁体   English   中英

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

[英]MVVM Binding to Custom Control in 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/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); }
    }
}

现在问题是当我尝试将它数据绑定到我创建的View Model时,没有任何反应。 各种代码项如下:

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为根(无论是Window还是UserControl)。 这意味着

<TextBlock Text="{Binding MyLabel}" />

尝试绑定到DataContext。 所以,基本上,你试图绑定的属性是

MyControl.DataContext.MyLabel

您需要绑定到以下内容:

MyControl.MyLabel

因此,您必须将绑定重新绑定到要绑定的可视树中的元素 MyControl类。 你可以通过几种方式做到这一点......

<TextBlock Text="{Binding MyLabel, 
                 RelativeSource={RelativeSource FindAncestor,
                                                AncestorType=UserControl}" />

是常见的,但必须走树,可能会导致一些性能问题。 我发现执行以下操作更容易,并建议:

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

为UserControl指定一个名称,然后使用ElementName将绑定重新绑定到可视树的根目录。

我将忽略其他更疯狂的方法来实现这一目标。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM