在您的 NavUserControl 中,我会为下一个和后退按钮连接事件或命令(或两者兼而有之)。然后您可以从 MainWindow 中访问它们并将适当的值设置到 Source 属性中。
如果你走事件路线,附加到事件上并直接设置源。
如果您走命令路线,请在您的视图模型中设置一个命令,将其绑定到用户控件,然后将 Source 属性绑定到您的视图模型中的另一个值。
编辑:根据 OP 的要求添加一些代码。请记住,这并不是最佳实践。只是一些例子。
走事件路线应该是最简单的。你已经知道如何做到这一点,我想。只需添加:
public event EventHandler BackClicked;
public event EventHandler NextClicked;
private void Back_Click(object sender, RoutedEventArgs e)
{
BackClicked(sender, e);
}
private void Next_Click(object sender, RoutedEventArgs e)
{
NextClicked(sender, e);
}
事件到您的 NavUserControl。然后将您的 XAML 更改为:
<StackPanel x:Name="LayoutRoot" Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Center" Margin="0,0,0,20">
<Button Content="Back" HorizontalAlignment="Left" Width="75" Click="Back_Click" />
<Button Content="Next" HorizontalAlignment="Left" Width="75" Click="Next_Click" />
</StackPanel>
现在在您的 MainWindow.xaml.cs 文件中,添加:
private void BackClicked(object sender, EventArgs e)
{
Uri source = // Whatever your business logic is to determine the previous page;
_Frame.Source = source;
}
private void NextClicked(object sender, EventArgs e)
{
Uri source = // Whatever your business logic is to determine the next page;
_Frame.Source = source;
}
并将 MainWindow XAML 更改为:
<Grid x:Name="LayoutRoot">
<Frame x:Name="_Frame" Content="Frame"
Source="/WpfApplication1;component/Page1.xaml"/>
<local:NavUserControl HorizontalAlignment="Center" VerticalAlignment="Bottom"
BackClicked="BackClicked" NextClicked="NextClicked" />
</Grid>
走命令路线需要更多的架构,但更干净。我建议使用您最喜欢的 MVVM 工具包。我最喜欢的是MVVMLight,所以我将在这个例子中使用它。
创建一个 ViewModel 类,如下所示:
public class ViewModel : GalaSoft.MvvmLight.ViewModelBase
{
private Uri _Source;
public Uri Source
{
get { return _Source; }
set
{
if (_Source != value)
{
_Source = value;
RaisePropertyChanged("Source");
}
}
}
private GalaSoft.MvvmLight.Command.RelayCommand _BackCommand;
public ICommand BackCommand
{
get
{
if (_BackCommand == null)
{
_BackCommand = new GalaSoft.MvvmLight.Command.RelayCommand(() =>
{
Uri source = // Whatever your business logic is to determine the previous page
Source = source;
});
}
return _BackCommand;
}
}
private GalaSoft.MvvmLight.Command.RelayCommand _NextCommand;
public ICommand NextCommand
{
get
{
if (_NextCommand == null)
{
_NextCommand = new GalaSoft.MvvmLight.Command.RelayCommand(() =>
{
Uri source = // Whatever your business logic is to determine the next page
Source = source;
});
}
return _NextCommand;
}
}
}
在 MainWindow.xaml.cs 中,创建此类的实例并将 DataContext 属性设置为该实例。然后设置你的绑定:
<Grid x:Name="LayoutRoot">
<Frame Content="Frame" Source="{Binding Source}"/>
<local:NavUserControl HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
</Grid>
和
<StackPanel x:Name="LayoutRoot" Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Center" Margin="0,0,0,20">
<Button Content="Back" HorizontalAlignment="Left" Width="75" Command="{Binding BackCommand}"/>
<Button Content="Next" HorizontalAlignment="Left" Width="75" Command="{Binding NextCommand}" />
</StackPanel>
绑定示例是非常简单的 MVVM 样式 WPF。我建议您走那条路,如果您需要更多帮助,请阅读 WPF 中的 MVVM。以教程和书籍的形式提供大量资源。 Searching here on SO 也有很大帮助。
再次编辑:
将您的构造函数更改为:
public MainWindow()
{
this.InitializeComponent();
// Insert code required on object creation below this point.
DataContext = new ViewModel();
}