在你NavUserControl,我將線了兩個事件或命令(或兩者兼而有之,也許是)未來和後退按鈕。然後,您可以從MainWindow中訪問這些文件,並將相應的值設置爲Source屬性。
如果您轉到活動路線,請將其附加到活動並直接設置來源。
如果你走命令路由,在你的viewmodel中設置一個命令,將它綁定到usercontrol,並將Source屬性綁定到viewmodel中的另一個值。
編輯:根據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;
}
,改變主窗口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,所以這就是我將用於這個例子。
創建一個視圖模型類,像這樣:
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();
}
謝謝你的提示。在正常工作的樣本中看到這一點將非常棒。 – vladc77 2011-05-24 19:44:54
哪些是您感興趣的?基於事件還是基於命令?或兩者? – Tim 2011-05-24 19:55:44
我想學習兩種方法。但是,我不想打擾你太多。 – vladc77 2011-05-24 20:07:58