从另一个 ViewModel 更改 ViewModel 的 属性

Changing the ViewModel's property from another ViewModel

我的 MainView.xaml 上有 ListBox,选择 Item 会强制 ContentControl 显示不同的 UserControl。我在这个程序中使用 Caliburn.Micro 库。这是一些代码:

    <ListBox Grid.Row="1" Grid.Column="1" x:Name="ItemsListBox" SelectedItem="0" ItemsSource="{Binding Items}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding TextBlock1Text}" x:Name="TextBlock1"/>
    <ContentControl Grid.Row="3" Grid.Column="1" Content="{Binding ElementName=ItemsListBox, Path=SelectedItem.Content}" />

MainViewModel.cs:

    private string _name;
    public string Name
    {
        get => _name;
        set
        {
            _name = value;
            NotifyOfPropertyChange(() => Name);
        }
    }
    private string _textBlock1Text;
    public string TextBlock1Text
    {
        get => _textBlock1Text;
        set
        {
            _textBlock1Text = value;
            NotifyOfPropertyChange(() => TextBlock1Text);
        }
    }
    public MainViewModel()
    {
        TextBlock1Text = "Test";
        Items = new ObservableCollection<ItemsModel>()
        {
            new ItemsModel { Name="Useless", Content=null },
            new ItemsModel { Name="TextChangerViewModel", Content=new TextChangerViewModel(TextBlock1Text) }
        };
    }
    public ObservableCollection<ItemsModel> Items { get; set; }

ItemsModel.cs:

    public class ItemsModel
    {
        public string Name { get; set; }
        public object Content { get; set; }
    }

最后 TextChangerViewModel.cs:

    public class TextChangerViewModel : Conductor<object>
{
    private string _textBlock1Text;

    public string TextBlock1Text
    {
        get => _textBlock1Text;
        set
        {
            _textBlock1Text = value;
            NotifyOfPropertyChange(() => TextBlock1Text);
        }
    }

    public TextChangerViewModel(string textBlock1Text) //passing parameter from another ViewModel
    {
        TextBlock1Text = textBlock1Text;
    }
}

因此,主要问题 是如何更改 MainViewModel.cs 中的 TextBlock1Text(以及 .xaml 中 TextBlock 的文本值)来自 TextChangerViewModel.cs?我正在考虑在我的 Items ObservableCollection 上使用 NotifyCollectionChanged 之类的东西,但它适用于 ItemsModel 的集合,而不适用于 VM,所以我被困在这里。

如果我的目标是 MVVM 模式,我也不确定在 ItemsModel.cs 中使用 public object Content { get; set; } 是否是一件好事,但我不知道其他方法(我对 MVVM 很陌生)。

UPD

我正在寻找 属性 更改方式,因为我需要从另一个 UserControl 更改 TextBlock1Text 文本。假设我的 TextChangerView.xaml 上有按钮:<Button Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Content="Change da text" cal:Message.Attach="ChangeTextButton"/>

点击它后,我希望更改父 MainView.xaml 上的文本。但问题是,我不知道在这种情况下如何更改属性,正如我在上面写的那样。

更改 textblox1 的绑定以引用所选项目。

<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding ElementName=ItemsListBox, Path=SelectedItem.Name}" x:Name="TextBlock1"/>

<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding ElementName=ItemsListBox, Path=SelectedItem.Content.TextBlock1Text}" x:Name="TextBlock1"/>