C# WPF导航和销毁相应的用户控制
我正试图在我的WPF应用程序中构建一个小型导航系统。我使用教程在页面之间导航。我想为一个C# WPF导航和销毁相应的用户控制,c#,wpf,xaml,user-controls,C#,Wpf,Xaml,User Controls,我正试图在我的WPF应用程序中构建一个小型导航系统。我使用教程在页面之间导航。我想为一个UserControl添加“返回”功能 我有一个UserControlOrders和另一个UserControlOrder订单显示在主窗口中,当我单击按钮时,订单用户控制应显示在主窗口中的相同位置。我试图在Orderusercontrol中引用Ordersusercontrol,并通过Order导航到Orders。但是,顺序没有被破坏,因为我使用了该类中的一个变量 如何确保当我导航到订单表单订单时,订单未被销
UserControl
添加“返回”功能
我有一个UserControlOrders
和另一个UserControlOrder
<代码>订单显示在主窗口中,当我单击按钮时,订单
用户控制
应显示在主窗口中的相同位置。我试图在Order
usercontrol中引用Orders
usercontrol,并通过Order
导航到Orders
。但是,顺序
没有被破坏,因为我使用了该类中的一个变量
如何确保当我导航到订单
表单订单
时,订单
未被销毁,当我从订单
导航到订单
时,订单
被销毁
按钮单击Orders类中的事件处理程序:
private void ShowOrder(object sender, RoutedEventArgs e)
{
Order order = new Order();
Switcher.Switch(order);
}
返回按钮单击订单类中的处理程序
public UserControl parent;
private void ReturnBack(object sender, RoutedEventArgs e)
{
Switcher.Switch(parent);
}
我通常使用ControlTemplate执行下一个模式,假设您的类中有:
private Enums.View _currView;
public Enums.View CurrView
{
get
{
return _currView;
}
set
{
_currView = value;
OnPropertyChanged("CurrView");
}
}
当Enums.View为:
public enum View
{
ViewA = 1,
ViewB = 2,
ViewC = 3,
}
然后,使用上面对CurrView的绑定,我们在视图更改时更改视图:
<UserControl ...
xmlns:Views="clr-namespace:CustomersManager.View"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<!--*********** Control templates ***********-->
<ControlTemplate x:Key="DefultTemplate">
<Views:DefultCustomersView/>
</ControlTemplate>
<ControlTemplate x:Key="A">
<Views:ViewAllCustomersView />
</ControlTemplate>
<ControlTemplate x:Key="B">
<Views:AddNewCustomersView />
</ControlTemplate>
<ControlTemplate x:Key="C">
<Views:EditCustomersView />
</ControlTemplate>
</UserControl.Resources>
<Border BorderBrush="Gray" BorderThickness="2">
<Grid>
<ContentControl DataContext="{Binding}" >
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="Template" Value="{StaticResource DefultTemplate}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=CurrView}" Value="ViewA">
<Setter Property="Template" Value="{StaticResource A}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurrView}" Value="ViewB">
<Setter Property="Template" Value="{StaticResource B}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurrView}" Value="ViewC">
<Setter Property="Template" Value="{StaticResource C}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl >
</Grid>
</Border>
</UserControl>