【问题标题】:Changing the ViewModel's property from another ViewModel从另一个 ViewModel 更改 ViewModel 的属性
【发布时间】:2018-09-01 21:32:12
【问题描述】:

我的 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;
    }
}

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

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

UPD

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

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

【问题讨论】:

    标签: c# wpf mvvm caliburn.micro


    【解决方案1】:

    更改 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"/>
    

    【讨论】:

    • 谢谢,这是一个很好的方法,但我正在寻找改变属性的方法。让我们扩展这个例子。假设我的 TextChangerView.xaml 上有按钮,并且我希望单击此按钮会导致 TextBlock1Text(在父 MainView.xaml 上)文本发生更改。我也会更新问题,抱歉造成误解。
    • 您是否尝试访问 TextChangerViewModel 中的属性?为此,我添加了一个示例。
    • 是的,这似乎解决了我的问题,谢谢。这意味着我不需要 MainViewModel.cs 中的 TextBlockText1 属性吗?在虚拟机中没有相应的属性(对于当前视图中的元素)似乎很奇怪,但在附属虚拟机中却有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-06
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多