gpt4 book ai didi

c# - 将按钮绑定(bind)到不同的 ViewModel [MVVM]

转载 作者:行者123 更新时间:2023-12-03 10:15:37 26 4
gpt4 key购买 nike

我在一个 View 中有两个按钮,是否可以将一个按钮绑定(bind)到第一个 ViewModel 中的一个命令,另一个按钮绑定(bind)到第二个 ViewModel 中的第二个命令?只需添加两个 ViewModel 都已初始化。

    <Button Command="{Binding ElementName=MainTreeView, Path=DataContext.FirstCommand}" CommandParameter="{Binding NameE}">
<Label Content="{Binding NameE}"/>
</Button>
<Button Command="{Binding ElementName=MainTreeView, Path=DataContext.SecondCommand}" CommandParameter="{Binding NameD}">
<Label Content="{Binding NameD}"/>
</Button>

还有我的两个 ViewModel:
public class FirstViewModel : INotifyPropertyChanged
{
public MyICommand<string> FirstCommand { get; private set; }

public HomeViewModel()
{
FirstCommand = new MyICommand<string>(myFirstCommand);
}

public void myFirstCommand(string par)
{
Console.Beep();
}
}


public class SecondViewModel : INotifyPropertyChanged
{
public MyICommand<string> SecondCommand { get; private set; }

public HomeViewModel()
{
SecondCommand = new MyICommand<string>(mySecondCommand);
}

public void mySecondCommand(string par)
{
Console.Beep();
}
}

编辑:在我有按钮的 View 中 <ContentPresenter Content="{Binding Content}"/>并且内容有 View 和 ViewModel 我想用按钮到达

最佳答案

最简单和最通用的解决方案是通过在 App.xaml ResourceDictionary 中实例化您的 View 模型来使它们全局可访问。 :

应用程序.xaml:

<Application ...>
<Application.Resources>
<ResourceDictionary>
<FirstViewModel x:Key="FirstViewModel" />
<SecondViewModel x:Key="SecondViewModel" />
</ResourceDictionary>
</Application.Resources>
</Application>

或使用 C#(例如,App.xaml.cs)
Application.Current.Resources.Add("FirstViewModel", new FirstViewModel(arg1, arg2));
Application.Current.Resources.Add("SecondViewModel", new SecondViewModel(arg1, arg2));

现在您可以从任何 View 引用它们:
<Button Content="{Binding NameE}" 
CommandParameter="{Binding NameE}"
Command="{Binding Source={StaticResource FirstViewModel}, Path=FirstCommand}" />

<Button Content="{Binding NameD}"
CommandParameter="{Binding NameD}"
Command="{Binding Source={StaticResource SecondViewModel}, Path=SecondCommand}" />

另一种解决方案是使用组合并将 View 模型包装到父 View 模型中:
public class MainViewModel : INotifyPropertyChanged
{
public FirstViewModel FirstViewModel { get; private set; }
public SecondViewModel SecondViewModel { get; private set; }

public MainViewModel()
{
this.FirstViewModel = new FirstViewModel();
this.SecondViewModel = new SecondViewModel();
}
}

public class FirstViewModel : INotifyPropertyChanged
{
public MyICommand<string> FirstCommand { get; private set; }

public FirstViewModel()
{
FirstCommand = new MyICommand<string>(myFirstCommand);
}

public void myFirstCommand(string par)
{
Console.Beep();
}
}


public class SecondViewModel : INotifyPropertyChanged
{
public MyICommand<string> SecondCommand { get; private set; }

public SecondViewModel()
{
SecondCommand = new MyICommand<string>(mySecondCommand);
}

public void mySecondCommand(string par)
{
Console.Beep();
}
}

用法
<TreeView x:Name="MainTreeView">
<TreeView.DataContext>
<MainViewModel />
</TreeView.DataContext>
</TreeView>


<Button Content="{Binding NameE}"
CommandParameter="{Binding NameE}"
Command="{Binding ElementName=MainTreeView, Path=DataContext.FirstViewModel.FirstCommand}" />

<Button Content="{Binding NameD}"
CommandParameter="{Binding NameD}"
Command="{Binding ElementName=MainTreeView, Path=DataContext.SecondViewModel.SecondCommand}" />

关于c# - 将按钮绑定(bind)到不同的 ViewModel [MVVM],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59405921/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com