【问题标题】:Bind Textblock to Textbox in MVVM Light在 MVVM Light 中将文本块绑定到文本框
【发布时间】:2012-05-15 09:48:51
【问题描述】:

我有一个非常简单的 MVVM Light 应用程序,它有一个文本块和一个文本框。我要做的是使用 MVVM Light Toolkit 和数据绑定将文本块值设置为文本框值。当我运行程序时,文本块文本没有更新。

private string _name = "Test Name";

public string Name
{                
    get
    {
        return _name;
    }
    set
    {
        _name = value;
        RaisePropertyChanged("NameChanged");
    }
}

这是 XAML。 “测试名称”看起来不错,但不会更改为文本框的值。 文本块:

<TextBlock x:Name="NameTitle"
           Text="{Binding Name}"
           Margin="-3,-8,0,0"/>

文本框:

<TextBox Text="{Binding Name, Mode="TwoWay"}" x:Name="tb"            
       HorizontalAlignment="Center"
       VerticalAlignment="Center"
       FontSize="40" >
       <i:Interaction.Triggers>
           <i:EventTrigger EventName="LostFocus">
             <commands:EventToCommand Command="{Binding Name}" 
                       CommandParameter="{Binding Text, ElementName=tb}" />
           </i:EventTrigger>
       </i:Interaction.Triggers>
</TextBox>

【问题讨论】:

    标签: silverlight xaml data-binding mvvm mvvm-light


    【解决方案1】:

    您应该将“Name”传递给 RaisePropertyChanged,而不是“NameChanged”。另一种解决方案是使用 ElementName 将 TextBlock 直接绑定到文本框

    XAML:

    <TextBlock x:Name="NameTitle" Text="{Binding ElementName=tb, Path=Text}" Margin="-3,-8,0,0"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      • 2015-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-04
      相关资源
      最近更新 更多