【问题标题】:MVVM Binding to Custom Control in Windows PhoneMVVM 绑定到 Windows Phone 中的自定义控件
【发布时间】:2011-11-01 06:11:31
【问题描述】:

我有以下为 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>

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

任何帮助将不胜感激

【问题讨论】:

  • 你想在 TextBlock 中显示 MyLabel.Text 还是什么?

标签: mvvm windows-phone


【解决方案1】:

(这是 WPF;我不确定这是否 100% 类似于 WP7 silverlight;我提供了更多详细信息以帮助您了解正在发生的事情,以便您可以研究合适的解决方案,如果我的两个选项不是' t 可用)

默认情况下,绑定以 xaml 文件的根元素(无论是 Window 还是 UserControl)的 DataContext 为根。这意味着

<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 将您的绑定重新绑定到可视化树的根目录。

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

【讨论】:

    猜你喜欢
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多