【问题标题】:UserControl Binding MVVM in Windows Phone 8Windows Phone 8 中的 UserControl 绑定 MVVM
【发布时间】:2014-04-27 21:36:43
【问题描述】:

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

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

文本块具有很好的价值,我可以让文本做类似的事情: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;
    }
}

【问题讨论】:

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


    【解决方案1】:
    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"); 
     }
    }
    

    【讨论】:

    • 查看我的编辑。我的 NotifyPropertyChanged 不是问题,它可以完美运行,但我无法将“myText”(在我的视图模型中)与我的用户控件中文本框的文本属性绑定。
    • 控件 是您创建的自定义 textox 控件,您确定它支持属性绑定吗?
    • 我想..我使用windows phone 8 sdk的文本框控件。我可以使用 设置文本,但不能使用
    猜你喜欢
    • 1970-01-01
    • 2014-01-15
    • 2013-12-30
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 2013-09-23
    相关资源
    最近更新 更多