gpt4 book ai didi

WPF - 如何处理从 MainWindow 中的 UserControl 引发的事件并更新 ViewModel

转载 作者:行者123 更新时间:2023-12-04 14:42:57 25 4
gpt4 key购买 nike

我试图在 UserControl1 中引发一个事件,但我无法弄清楚如何处理该事件以在 MainWindow 中显示 UserControl2。

主窗口.xaml

<Window x:Class="WpfApp1.MainWindow"
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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">

<Window.DataContext>
<local:ViewModel1 />
</Window.DataContext>

<Window.Resources>
<DataTemplate x:Key="Template1" DataType="{x:Type local:ViewModel1}">
<local:UserControl1 />
</DataTemplate>
<DataTemplate x:Key="Template2" DataType="{x:Type local:ViewModel1}">
<local:UserControl2 />
</DataTemplate>
</Window.Resources>
<Grid>
<ContentControl Content="{Binding }">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate" Value="{StaticResource Template1}" />
<Style.Triggers>
<DataTrigger Binding="{Binding UserControlId}" Value="2">
<Setter Property="ContentTemplate" Value="{StaticResource Template2}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</Grid>
</Window>

主窗口.cs

using System.Windows;

namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}

UserControl1.xaml

<UserControl x:Class="WpfApp1.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"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Label Content="User Control 1" />
<Button x:Name="btnNext" Content="Next" Click="btnNext_Click"></Button>
</Grid>
</UserControl>

用户控件1.cs

using System.Windows;
using System.Windows.Controls;

namespace WpfApp1
{
public partial class UserControl1 : UserControl
{
public static readonly RoutedEvent MyEvent = EventManager.RegisterRoutedEvent(
"MyEventName",
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(UserControl1)
);

public event RoutedEventHandler LoginEventHandler
{
add { AddHandler(MyEvent, value); }
remove { RemoveHandler(MyEvent, value); }

}
public UserControl1()
{
InitializeComponent();
}

private void btnNext_Click(object sender, RoutedEventArgs e)
{
var eventArgs = new RoutedEventArgs(MyEvent);
RaiseEvent(eventArgs);
}
}
}

UserControl2.xaml

<UserControl x:Class="WpfApp1.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"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Label Content="User Control 2" />
</Grid>
</UserControl>

UserControl2.cs

using System.Windows.Controls;

namespace WpfApp1
{
public partial class UserControl2 : UserControl
{
public UserControl2()
{
InitializeComponent();
}
}
}

ViewModel1.cs

using System.ComponentModel;

namespace WpfApp1
{
public class ViewModel1 : INotifyPropertyChanged
{
public int UserControlId { get; set; }

public ViewModel1()
{
UserControlId = 2;
}

public event PropertyChangedEventHandler PropertyChanged;
}
}

我想我必须更新 ViewModel1 中的 UserControlId 属性来更改用户控件,但是要处理哪里以及如何获取 ViewModel1 实例来更改属性?

编辑 1抱歉,ViewModel1 构造函数中的拼写错误,我的意思是将 UserControlId 属性初始化为 1,而不是 2。

当我运行它时,我看到主窗口中加载了 UserControl1 的窗口。UserControl1 有一个按钮“下一步”。单击 UserControl1 中的按钮,我想显示 UserControl2。所以我在按钮单击事件处理程序中引发 MyEvent 路由事件。现在,在哪里附加和编写 MyEvent 的事件处理程序以及如何让 ViewModel1 实例更改它的 UserControlId 属性?

最佳答案

我终于让它工作了。引发的事件在 ViewModel1 中更改了属性名称,在 MainWindow 中添加了事件监听器,

这里是修改后的文件

ViewModel1.cs

using System.ComponentModel;

namespace WpfApp1
{
public class ViewModel1 : INotifyPropertyChanged
{
private int _userControlId;

public int UserControlId {
get { return _userControlId; }

set
{
_userControlId = value;
RaisePropertyChanged("UserControlId");
}
}

private void RaisePropertyChanged(string v = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(v));
}

public ViewModel1()
{
UserControlId = 1;
}

public event PropertyChangedEventHandler PropertyChanged;
}
}

主窗口.cs

using System.Windows;

namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

AddHandler(UserControl1.MyEvent, new RoutedEventHandler(OnMyEvent));
}

private void OnMyEvent(object sender, RoutedEventArgs e)
{
var vm = (ViewModel1)DataContext;
vm.UserControlId = "2";
}
}
}

关于WPF - 如何处理从 MainWindow 中的 UserControl 引发的事件并更新 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47406364/

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