0

我有一个非常简单的 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>
4

1 回答 1

4

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

XAML:

<TextBlock x:Name="NameTitle" Text="{Binding ElementName=tb, Path=Text}" Margin="-3,-8,0,0"/>
于 2012-05-06T17:46:10.153 回答