在WPF中获取Textblock以使用MVVM进行更新

时间:2017-06-13 07:40:28

标签: c# wpf xaml mvvm

所以我刚开始使用MVVM玩WPF并遇到了问题。

我想创建一个简单的程序,它接受一个名字和姓氏的列表,并将一个随机名称放在一起。一切正常,但名称未显示在应用程序窗口中。

按钮命令被正确委托给我的函数,并且在调试时引发了PropertyChanged事件。

我的代码如下所示:

视图模型:

class NamePicker: ObservableObject
{
    private string _name;

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

    public ICommand CreateNameCommand
    {
        get { return new DelegateCommand(CreateName); }
    }

    public void CreateName()
    {
        Random rnd =new Random();
        Name = randomNames.firstName[rnd.Next(0, randomNames.firstName.Length - 1)] + " "
                + randomNames.surName[rnd.Next(0, randomNames.surName.Length - 1)];
    }

}

ObservableObject:

public class ObservableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChangedEvent(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

XAML:

<Window
    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"
    xmlns:local="clr-namespace:MVVM_Beispiel"
    xmlns:ViewModels="clr-namespace:MVVM_Beispiel.ViewModels" x:Class="MVVM_Beispiel.MainWindow"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button Command="{Binding CreateNameCommand}" Content="Name ausgeben" HorizontalAlignment="Left" Margin="202,292,0,0" VerticalAlignment="Top" Width="112">
        <Button.DataContext>
            <ViewModels:NamePicker/>
        </Button.DataContext>
    </Button>
    <Label Content="Namensgenerator" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="202,10,0,0" Width="112"/>
    <Image HorizontalAlignment="Left" Height="183" Margin="140,41,0,0" VerticalAlignment="Top" Width="244" Source="/MVVM-Beispiel;component/img/007.jpg"/>
    <TextBlock HorizontalAlignment="Left" Margin="140,251,0,0" TextWrapping="Wrap" Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="244">
        <TextBlock.DataContext>
            <ViewModels:NamePicker/>
        </TextBlock.DataContext>
    </TextBlock>

</Grid>

DelegateCommand:

  public class DelegateCommand : ICommand
{
    private readonly Action _action;

    public DelegateCommand(Action action)
    {
        _action = action;
    }

    public void Execute(object parameter)
    {
        _action();
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }
}

那么究竟我错过了TextBlock没有显示当前生成的名称?我阅读了一些教程,但那些没有详细介绍所有内容,似乎我应该找到一本关于MVVM的好书。

1 个答案:

答案 0 :(得分:1)

您有NamePicker的两个不同实例:Button中的第一个和TextBlock中的第二个Name。所以<Button.DataContext> <ViewModels:NamePicker/> </Button.DataContext> <TextBlock.DataContext> <ViewModels:NamePicker/> </TextBlock.DataContext> 仅在第一个中被更改。删除所有这些东西:

<Window.DataContext>
    <ViewModels:NamePicker/>
</Window.DataContext>

并将一个视图模型设置为整个窗口:

URLEncoder.encode(myUrl,"UTF-8")