WPF:使用框架技术从列表框菜单进行页面到页面的导航? [英] WPF: Page to page navigation from listbox menu employing frame technique?

查看:49
本文介绍了WPF:使用框架技术从列表框菜单进行页面到页面的导航?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了问题.我在窗口xaml中添加了一个框架以加载页面.我可以使用框架的Source标签将页面直接加载到框架中.有用.我需要使用C#中的代码来引用列表框菜单中的链接,并在选择列表框项目时填充适当的链接.我的问题是我无法在C#代码中引用该框架,只是看不到它.我用x:Name ="ContentFrame"定义了框架.当我在C#中引用时,Intellisense会告知名称"ContentFrame"在当前上下文中不存在".我做错了什么?我在这里迷路了.任何想法都受到高度赞赏.这是代码:

I got a problem. I added a frame in the window xaml to load pages in. I can directly load a page into frame with Source tag of the frame. It works. I need to use the code in C# to refer to the link from listbox menu a poplulate an apropriate link when an listbox item is selected. My problem is that I cannot refer the frame in C# code, it just cannot be seen. I defined the frame with x:Name="ContentFrame". When I refer to in in C#, Intellisense tells that "The name "ContentFrame" does not exist in the current context". What I am doing wrong? I am lost here. Any ideas are highly appreciated. Here is the code:

<框架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;
    }
}

`

推荐答案

您做得差不多.唯一的问题是绑定到所选项目.由于框架的Source属性是Uri类型,并且没有动态转换器,因此您需要一个自己的转换器来完成工作:

You did it almost right. The only problem is the binding to the selected item. Since the Source property of the frame is of type Uri, and has no dynamic converter, you need an own converter, which does the job:

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();
    }
}

现在,您无需使用xpath即可直接绑定所选项目,转换器将提取uri字符串并构建一个Uri对象.这是完全可用的xaml(除了转换器之外,没有任何代码):

You now bind the selected item directly with no xpath and the converter extracts the uri string and builds an Uri object. Here is the fully working xaml (no code behind, except the converter):

<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>

页面必须位于窗口同一目录的pages文件夹中.在我的示例中,我有两个页面带有TextBlock我是Page1/Page2".

The pages have to be in the pages folder of the same directory of the window of course. In my example, I had two pages with a TextBlock "I am Page1/Page2".

希望我能为您提供帮助:)

Hope i could help you :)

这篇关于WPF:使用框架技术从列表框菜单进行页面到页面的导航?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆