WPF从窗口中的帧导航到Usercontrol并将参数传递给它

时间:2016-02-11 09:54:14

标签: c# wpf xaml parameters user-controls

我在WPF / C#应用程序中有一个现代窗口,我在其中添加了一个现代框架:

<mui:ModernWindow x:Class="MyApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mui="http://firstfloorsoftware.com/ModernUI"
    WindowStartupLocation="CenterScreen"
    Style="{StaticResource EmptyWindow}">

<Window.Resources>
</Window.Resources>
<Grid>
<Menu x:Name="menu" Height="62" VerticalAlignment="Top" >
        <MenuItem x:Name="miHome" Header="Home" Click="MenuItem_Home" IsChecked="True" Width="60" FontSize="14" />
        <MenuItem x:Name="miClients" Header="Clients"  FontSize="14" Click="MenuItem_Clients" Width="65"/>
        <MenuItem x:Name="miSuppliers" Header="Suppliers" FontSize="14"  Click="MenuItem_Suppliers" Width="81"/>
        <MenuItem x:Name="miReports" Header="Reports" FontSize="14"  Click="MenuItem_Reporting" Width="71"/>
    </Menu>
    <mui:ModernFrame Margin="0,75,10,10" x:Name="frame">

    </mui:ModernFrame>
</Grid>

我的应用程序中有MenuItems,当我点击Suppliers项目时,我用一个usercontrol填充框架,如下所示:

frame.Source = new Uri("/Pages/Suppliers.xaml", UriKind.Relative);

其中Suppliers.xaml的设计是:

<UserControl
             x:Class="MyApp.LinksBar.Suppliers"
             xmlns:MyApp="clr-namespace:MyApp" 
             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:mui="http://firstfloorsoftware.com/ModernUI"  
             mc:Ignorable="d" 
             d:DesignHeight="575" d:DesignWidth="905">
    <UserControl.Resources>
    </UserControl.Resources>
    <Grid  Name="Grid">
        <mui:ModernButton x:Name="btnmakePayment" Content="Make Payment" Click="btnMakePayment_Click" Grid.Row="2" HorizontalAlignment="Right" VerticalAlignment="Top"/>
    </Grid>
</UserControl>

当我点击&#34;付款&#34;按钮,我导航到另一个UserControl(MakePayment.xaml):

private void btnMakePayment_Click(object sender, RoutedEventArgs e)
{
    NavigationCommands.GoToPage.Execute(new Uri("/Actions/MakePayment.xaml", UriKind.Relative), this);
}

MakePayment.xaml设计是:

<UserControl
             xmlns:local="clr-namespace:MyApp.Actions" x:Class="MyApp.Actions.MakePayment" 
             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:mui="http://firstfloorsoftware.com/ModernUI" xmlns:System="clr-namespace:System;assembly=mscorlib" xmlns:MyApp="clr-namespace:MyApp" 
             mc:Ignorable="d"
             Loaded="MakePayment_Loaded"
             d:DesignHeight="600" d:DesignWidth="866" >
    <UserControl.Resources>
    </UserControl.Resources>
    <Grid DataContext="{StaticResource makePaymentViewSource}" Name="Grid">
        <Grid.RowDefinitions>
        </Grid.RowDefinitions>
    <Label Content="Total"  VerticalContentAlignment="Center" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    // More design code here ...
    </Grid>
</UserControl>

我的问题出现了: 我需要将参数从Suppliers UserControl传递给MakePayment UserControl 。 如何以编程方式传递供应商中的参数并在MakePayment中读取它们?

谢谢。

1 个答案:

答案 0 :(得分:0)

如果你可以将一个绑定到另一个,那就是你应该做的。到目前为止,最简单的方法是进行两次UserControls&#34;沟通&#34; (或共享两者都可以做的事情)是在每个属性上定义DependencyProperty并将它们双向绑定。

这样,两者都可以访问相同的值,并且两者都可以使用它。

以我自己的控制为例:

<UserControl x:Class="MyControls.MasterContainerControl"
            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:MyControls"
            mc:Ignorable="d"
            x:Name="masterContainerControl">
    <local:ContainerControl SomeProperty="{Binding ElementName=masterContainerControl, Path=SomeProperty}">
        <local:ContainerControl.Another>
            <local:AnotherControl SomeProperty="{Binding ElementName=masterContainerControl, Path=SomeProperty"/>
        </local:ExplorerBase.AddressBar>
        <local:ContainerControl.Some>
            <local:SomeControl SomeProperty="{Binding ElementName=masterContainerControl, Path=SomeProperty"/>
        </local:ContainerControl.Some>  
    </local:ContainerControl>
</UserControl>

当然,这一切都假设MasterContainerControlContainerControlAnotherControlSomeControl都有一个名为SomeProperty的DependencyProperty,然后绑定密封这笔交易。

注意:确保在MasterContainerControl中定义默认值,因为它们将覆盖MasterContainerControl绑定的值。

如果我误解了您的问题,请告诉我。