【问题标题】:Get Textblock in WPF to update using MVVM在 WPF 中获取 Textblock 以使用 MVVM 进行更新
【发布时间】:2017-06-13 07:40:28
【问题描述】:

所以我刚开始使用 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 的好书。

【问题讨论】:

    标签: c# wpf xaml mvvm


    【解决方案1】:

    您有两个不同的NamePicker 实例:第一个在Button 中,第二个在TextBlock 中。所以Name 仅在第一个中更改。删除所有这些东西:

    <Button.DataContext>
        <ViewModels:NamePicker/>
    </Button.DataContext>
    
    <TextBlock.DataContext>
        <ViewModels:NamePicker/>
    </TextBlock.DataContext>
    

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

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

    【讨论】:

      猜你喜欢
      • 2015-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-03
      • 2014-11-23
      • 1970-01-01
      相关资源
      最近更新 更多