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

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

问题描述

我遇到了问题.我在窗口 xaml 中添加了一个框架来加载页面.我可以使用框架的 Source 标签直接将页面加载到框架中.有用.我需要使用 C# 中的代码来引用列表框菜单中的链接,并在选择列表框项目时填充适当的链接.我的问题是我无法在 C# 代码中引用框架,它只是看不到.我用 x:Name="ContentFrame" 定义了框架.当我在 C# 中引用 in 时,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:

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

`

推荐答案

你做得几乎是对的.唯一的问题是与所选项目的绑定.由于框架的 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".

希望能帮到你:)

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

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