我有以下代码在WPF中切换ContentControl
的内容。我向它传递了“用户控制”页面的path值,但它不会加载新页面。
MainWindow.xaml.cs
public void SwitchPage(string pagePath)
{
MainContent.Content = pagePath;
}
MainWindow.xaml
MainWindow
包含以下ContentControl
声明:
<ContentControl Name="MainContent" Content="{Binding ElementName=DemoItemsListBox, Path=SelectedItem.Content}" />
以下是触发事件以将Url(用户控制路径)传递给函数的按钮:
MenuLinks.xaml
<DataTemplate DataType="{x:Type domain:MenuLink}">
<Button Name="ImageButton" ToolTip="Wiki Article" ToolTipService.InitialShowDelay="0"
Style="{DynamicResource MaterialDesignFlatButton}"
Click="Button_Click"
CommandParameter="{Binding Url}">
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon x:Name="PackIcon" Kind="BookOpenVariant" />
<TextBlock Margin="8 0 0 0" Style="{StaticResource MaterialDesignBody1TextBlock}"
Text="{Binding Label, Mode=OneWay}" />
</StackPanel>
</Button>
</DataTemplate>
该按钮如下调用MenuLinks.xaml.cs代码隐藏文件中的Button_Click
函数,该文件调用SwitchPage
中的MainWindow
函数:
MenuLinks.xaml.cs
private void Button_Click(object sender, RoutedEventArgs e)
{
Button _myButton = (Button)sender;
string value = _myButton.CommandParameter.ToString();
MainWindow window = new MainWindow();
window.SwitchPage(value);
}