相关疑难解决方法(0)

如何从其他视图模型中调用主视图模型中的函数?

我的节目由一个TreeView和两个contentPresenters地面组成.mainWindow,TreeView和每个contentPresenter都有自己的viewModels.

我想调用一个函数在mainWindowViewModelTreeViewViewModel.

我需要这样做,因为mainWindowViewModel控件显示的内容contentPresenters,我想手动更新显示.

我猜我会做这样的事......

TreeViewViewModel:

public class TreeViewViewModel
{
     //Do I need to declare the MainWindowVM?

     public TreeViewViewModel() { ... }

     private void function()
     {
          //Command that affects display

          //Manually call function in MainWindowVM to refresh View
     }
}
Run Code Online (Sandbox Code Playgroud)

我试图通过使用来访问MainWindowVM来自TreeViewViewModel:

public MainWindowViewModel ViewModel { get { return DataContext as MainWindowViewModel; } }
Run Code Online (Sandbox Code Playgroud)

但它没有多大意义.因为MWVM不是DataContextTreeViewViewModel.

c# wpf mvvm viewmodel

9
推荐指数
1
解决办法
1万
查看次数

WPF MVVM切换用户控件

我是MVVM和WPF的新手,但我知道MVVM中发生了什么.我在主窗口中切换用户控件时遇到问题.在我的应用程序中,我有:MainWindow.xaml,带有日志和2个链接:显示全部并创建新的.当然我有ViewModel.我还有2个UserControls:ShowAll和Create with ViewModels以及其中的所有逻辑(添加数据等).当我单击链接时创建新建或单击ShowAll时显示全部,如何显示创建表单?

在windowForms我只是隐藏UC,但是这里没有代码:)

我的MainWindow.xaml:

<Window x:Class="Test.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="300" Width="300">
    <Grid>
        <StackPanel>
            <TextBox Text="{Binding Name}"/>
            <Button Content="Change" Command="{Binding ChangeCommand}"/>
        </StackPanel>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

我的MainWindowViewModel:

class MainWindowViewModel : BaseViewModel
{
    private Person _person;
    private BaseCommand _changeCommand;

    public MainWindowViewModel()
    {
        _person = new Person();
    }

    public string Name
    {
        get
        {
            return _person.Name;
        }
        set
        {
            if (_person.Name != value)
                _person.Name = value;
            OnPropertyChanged(() => Name);
        }
    }

    public ICommand ChangeCommand
    {
        get
        {
            if (_changeCommand == null)
                _changeCommand = new …
Run Code Online (Sandbox Code Playgroud)

c# wpf user-controls mvvm

4
推荐指数
1
解决办法
1万
查看次数

用户控件之间的WPF通信

我正在尝试找到在两个用户控件之间进行通信的最佳方法。我有一个XAML主窗口,其中包含两个用户控件,而这些用户控件又包含各种控件。每个用户控件后面的代码只是将DataContext设置为与其关联的视图模型。视图模型包含绑定到控件的对象。我想做的是在用户控件1中的列表框更改选择时捕获,新选择的项目显示在用户控件2中的编辑框中。当我使用视图模型时,无法声明依赖项属性,因此我想知道执行此操作的公认方法是什么?我已经附上了一些基本代码,以展示如何设置控件。

主窗口XAML

<Window x:Class="CommsTest.View.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CommsTest.View"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <local:UserControl1 />
    <local:UserControl2 />
</Grid>
Run Code Online (Sandbox Code Playgroud)

UserControl1 XAML

<UserControl x:Class="CommsTest.View.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <ComboBox Height="23" HorizontalAlignment="Left" Margin="50,110,0,0" Name="comboBox1" VerticalAlignment="Top" Width="199" ItemsSource="{Binding Combo1}" />
</Grid>
Run Code Online (Sandbox Code Playgroud)

UserControl1ViewModel.cs

class UserControl1ViewModel
{
    private ObservableCollection<string> combo1 = new ObservableCollection<string>();

    public ObservableCollection<string> Combo1
    {
        get { return combo1; }
    }
}
Run Code Online (Sandbox Code Playgroud)

UserControl2.XAML

<UserControl x:Class="CommsTest.View.UserControl2"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <TextBox Height="23" HorizontalAlignment="Left" Margin="63,84,0,0" Name="textBox1" VerticalAlignment="Top" Width="170" Text="{Binding Path=Text1}" …
Run Code Online (Sandbox Code Playgroud)

c# wpf mvvm

3
推荐指数
1
解决办法
5645
查看次数

标签 统计

c# ×3

mvvm ×3

wpf ×3

user-controls ×1

viewmodel ×1