vla*_*c77 5 navigation wpf frame
我遇到了问题.我在窗口xaml中添加了一个框架来加载页面.我可以直接将页面加载到框架的Source标签的框架中.有用.我需要使用C#中的代码来引用列表框菜单中的链接,在选择列表框项目时弹出适当的链接.我的问题是我不能在C#代码中引用框架,它只是无法看到.我用x:Name ="ContentFrame"定义了框架.当我在C#中引用时,Intellisense告诉"当前上下文中不存在名称"ContentFrame".我做错了什么?我迷失在这里.任何想法都受到高度赞赏.这是代码:
<Frame x:Name="ContentFrame" JournalOwnership="OwnsJournal" NavigationUIVisibility="Hidden" Grid.Column="2" </Frame>
private void SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
string itemName = lbi.Content.ToString();
if ( Nav_ListBox.SelectedItem.Equals("Page1" ) )
{
ContentFrame.Source = new Uri("Pages/Page1.xaml", UriKind.Relative);
Canvas_Frame.NavigationUIVisibility = NavigationUIVisibility.Hidden;
}
}
Run Code Online (Sandbox Code Playgroud)
`
你几乎做对了。唯一的问题是与所选项目的绑定。由于框架的 Source 属性是 Uri 类型,并且没有动态转换器,因此您需要一个自己的转换器来完成以下工作:
public class UriConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
XmlElement element = value as XmlElement;
if (element != null)
{
string uriSource = element.SelectSingleNode("source").InnerText;
return new Uri(uriSource, UriKind.Relative);
}
else
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以直接绑定所选项目,无需使用 xpath,转换器会提取 uri 字符串并构建一个 Uri 对象。这是完整工作的 xaml(除了转换器之外没有任何代码):
<Window x:Class="FrameTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FrameTest"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate x:Key="pageTemplate" >
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding XPath=name}" FontSize="14"
VerticalAlignment="Center" Margin="4" />
</StackPanel>
</DataTemplate>
<XmlDataProvider x:Key="PagesData" XPath="Pages">
<x:XData>
<Pages xmlns="">
<page id="page01">
<name>Page 1</name>
<source>Pages/Page1.xaml</source>
</page>
<page id="page02">
<name>Page 2</name>
<source>Pages/Page2.xaml</source>
</page>
</Pages>
</x:XData>
</XmlDataProvider>
<local:UriConverter x:Key="UriConverter" />
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListBox x:Name="Nav_ListBox" Grid.Column="0"
VerticalAlignment="Top"
TextBlock.Foreground="Black"
ItemTemplate="{DynamicResource pageTemplate}"
ItemsSource="{Binding Source={StaticResource PagesData},
XPath=page}"/>
<Frame NavigationUIVisibility="Hidden"
JournalOwnership="OwnsJournal" Grid.Column="1"
Source="{Binding ElementName=Nav_ListBox, Path=SelectedItem,
Converter={StaticResource UriConverter}}"/>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
当然,这些页面必须位于窗口同一目录的页面文件夹中。在我的示例中,我有两个带有 TextBlock“我是 Page1/Page2”的页面。
希望我能帮助你:)
| 归档时间: |
|
| 查看次数: |
8576 次 |
| 最近记录: |