简体   繁体   English

Windows Phone 8中的UserControl绑定MVVM

[英]UserControl Binding MVVM in Windows Phone 8

I have this a simple usercontrol with a textbox 我有一个带有文本框的简单用户控件

XAML: 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>

Code behind: 后面的代码:

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); }
    }
}

} }

And a page : 和一个页面:

<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>

In my viewmodel : 在我的视图模型中:

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

the textblock as the good value and I can get the text to do something like that : myusertextbox.Text textblock是一个很好的价值,我可以让文本做类似的事情:myusertextbox.Text

But I would like to set automatically myText with the value of the textbox in my usercontrol. 但是我想用用户控件中文本框的值自动设置myText。

I try all day but it still not working... 我整日尝试,但仍然无法正常工作...

What am I missing? 我想念什么? Thanks in advance. 提前致谢。

EDIT: 编辑:

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;
    }
}
private String mytext;
public String myText
{
   get { return myText; }
   set { NotifyPropertyChanged(ref myText, value); }
}

What kind of framework do you use for implementing INotifyPropertyChanged interface? 您使用哪种框架来实现INotifyPropertyChanged接口? You are not even setting the value to your private field here. 您甚至没有在此处将值设置为私有字段。 This looks quite strange to me. 这在我看来很奇怪。 It doesn't seem like it's following the get - set - notify route. 似乎并没有遵循get - set - notify route。

It is suppose to be something like: 它应该是这样的:

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

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

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