繁体   English   中英

Windows Phone 8中的UserControl绑定MVVM

[英]UserControl Binding MVVM in Windows Phone 8

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

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>

在我的视图模型中:

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

textblock是一个很好的价值,我可以让文本做类似的事情:myusertextbox.Text

但是我想用用户控件中文本框的值自动设置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;
    }
}
private String mytext;
public String myText
{
   get { return myText; }
   set { NotifyPropertyChanged(ref myText, value); }
}

您使用哪种框架来实现INotifyPropertyChanged接口? 您甚至没有在此处将值设置为私有字段。 这在我看来很奇怪。 似乎并没有遵循get - set - notify route。

它应该是这样的:

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