UserControl绑定Windows Phone 8中的MVVM

时间:2014-04-27 21:36:44

标签: c# xaml windows-phone-8 mvvm windows-phone

我有一个带文本框的简单用户控件

XAML:

<UserControl x:Name="myTextBox" x:Class="Views.UserControls.myTextBox"
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:DesignWidth="480" Height="92">

<StackPanel>
    <StackPanel Margin="25,0,0,0" Orientation="Horizontal">
        <TextBlock TextWrapping="Wrap" Text="Title" FontSize="16" HorizontalAlignment="Left"/>
    </StackPanel>
    <TextBox x:Name="textbox" TextWrapping="Wrap" Text="{Binding Text, Mode=TwoWay}" VerticalAlignment="Top"/>
</StackPanel>

代码背后:

public partial class LabeledTextBox : UserControl
{
    public LabeledTextBox()
    {
        InitializeComponent();
        DataContext = this;
    }

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(LabeledTextBox), new PropertyMetadata(default(String)));


    public String Text
    {
        get { return (String)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
}

}

还有一页:

<phone:PhoneApplicationPage
[...]
>

<phone:PhoneApplicationPage.DataContext>
    <viewmodel:myViewModel/>
</phone:PhoneApplicationPage.DataContext>

<StackPannel>
    <TextBlock Text="{Binding Text, ElementName=myusertextbox}"/>

    <usercontrols:myTextBox x:Name="myusertextbox" Text="{Binding myText, Mode=TwoWay}"/>
</StackPannel>

在我的viewmodel中:

private String mytext;
public String myText
{
   get { return myText; }
   set { NotifyPropertyChanged(ref myText, value); }
}

将textblock作为好的值,我可以让文本做类似的事情:myusertextbox.Text

但是我想在我的usercontrol中使用文本框的值自动设置myText。

我整日都在尝试,但它仍无法正常工作......

我错过了什么?提前谢谢。

修改

public abstract class ANotifyPropertyChanged : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string nomPropriete)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(nomPropriete));
    }

    public bool NotifyPropertyChanged<T>(ref T variable, T valeur, [CallerMemberName] string nomPropriete = null)
    {
        if (object.Equals(variable, valeur))
            return false;

        variable = valeur;
        NotifyPropertyChanged(nomPropriete);
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

private String mytext;
public String myText
{
   get { return myText; }
   set { NotifyPropertyChanged(ref myText, value); }
}

您使用什么样的框架来实现INotifyPropertyChanged接口? 您甚至没有在此处将值设置为您的私有字段。 这看起来很奇怪。它看起来并不像get - set - notify路线。

假设是这样的:

private String mytext;
public String myText
{
   get { return myText; }
   set 
 { 
   mytext = value;
   NotifyPropertyChanged("myText"); 
 }
}