none
listbox绑定数据,触摸不同的数据怎么转跳到不同页面 RRS feed

  • 问题

  • 
    <ListBox Height="387" HorizontalAlignment="Left" Margin="13,30,0,0" Name="listBox1" ItemsSource="{Binding Source={StaticResource CockTail},Path=DataCollection}" VerticalAlignment="Top" Width="357">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal" ManipulationStarted="StackPanel_ManipulationStarted">
                                        <Image  Source="{Binding Path=ImageUrl}"/>
                                        <StackPanel Orientation="Vertical">
                                            <TextBlock  Text="{Binding Color}"/>
                                            <TextBlock  Text="{Binding Name}"/>
                                            <TextBlock  Text="{Binding Cup}"/>
                                            <TextBlock  Text="{Binding BaseWine}"/>
                                        </StackPanel>
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>

    绑定代码如上,

    请问我怎么触摸下面图中的不同位置根据

    <TextBlock  Text="{Binding Name}"/>

    导航道不同页面


    
    
    
    2012年4月9日 13:35

答案

  • 你好,

    可以尝试将Url字段(即你需要导航到页面的Uri)绑定到Button的Tag属性上。部分示例代码如下:

    MainPage.xaml

     <ListBox Height="377" HorizontalAlignment="Left" Margin="142,59,0,0" Name="listBox1" VerticalAlignment="Top" Width="460"  ItemsSource="{Binding}" >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Button Width="460" Height="120" ClickMode="Press" Click="Button_Click" Tag="{Binding Url}">
                                <StackPanel>
                                    <TextBlock  Text="{Binding Name}"/>
                                    <TextBlock  Text="{Binding Cup}"/>
                                    <TextBlock  Text="{Binding BaseWine}"/>
                                </StackPanel>
                            </Button>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

    .cs

    public MainPage() { InitializeComponent(); this.DataContext = GridData.GetData(); } private void Button_Click(object sender, RoutedEventArgs e) { Button button = (Button)sender; string url = button.Tag.ToString(); NavigationService.Navigate(new Uri(url, UriKind.Relative)); }

    //数据类

    public class GridData
        {
            public string Name { get; set; }
            public int Cup { get; set; }
            public bool BaseWine { get; set; }
            public string Url { get; set; }
            public static ObservableCollection<GridData> GetData()
            {
                ObservableCollection<GridData> data = new ObservableCollection<GridData>();
                data.Add(new GridData()
                {
                    Name = "John Doe",
                    Cup = 1,
                    BaseWine = true,
                    Url = "/Page1.xaml"
                });
                data.Add(new GridData()
                {
                    Name = "Jane Doe",
                    Cup = 2,
                    BaseWine = false,
                    Url = "/Page2.xaml"
                });
                return data;
            }
        }

    希望有所帮助。

    • 已标记为答案 SoullessL 2012年4月11日 14:01
    2012年4月11日 1:59
    版主

全部回复

  • - -没人帮忙解决么?
    2012年4月10日 0:37
  • 你好,

    可以尝试将Url字段(即你需要导航到页面的Uri)绑定到Button的Tag属性上。部分示例代码如下:

    MainPage.xaml

     <ListBox Height="377" HorizontalAlignment="Left" Margin="142,59,0,0" Name="listBox1" VerticalAlignment="Top" Width="460"  ItemsSource="{Binding}" >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Button Width="460" Height="120" ClickMode="Press" Click="Button_Click" Tag="{Binding Url}">
                                <StackPanel>
                                    <TextBlock  Text="{Binding Name}"/>
                                    <TextBlock  Text="{Binding Cup}"/>
                                    <TextBlock  Text="{Binding BaseWine}"/>
                                </StackPanel>
                            </Button>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

    .cs

    public MainPage() { InitializeComponent(); this.DataContext = GridData.GetData(); } private void Button_Click(object sender, RoutedEventArgs e) { Button button = (Button)sender; string url = button.Tag.ToString(); NavigationService.Navigate(new Uri(url, UriKind.Relative)); }

    //数据类

    public class GridData
        {
            public string Name { get; set; }
            public int Cup { get; set; }
            public bool BaseWine { get; set; }
            public string Url { get; set; }
            public static ObservableCollection<GridData> GetData()
            {
                ObservableCollection<GridData> data = new ObservableCollection<GridData>();
                data.Add(new GridData()
                {
                    Name = "John Doe",
                    Cup = 1,
                    BaseWine = true,
                    Url = "/Page1.xaml"
                });
                data.Add(new GridData()
                {
                    Name = "Jane Doe",
                    Cup = 2,
                    BaseWine = false,
                    Url = "/Page2.xaml"
                });
                return data;
            }
        }

    希望有所帮助。

    • 已标记为答案 SoullessL 2012年4月11日 14:01
    2012年4月11日 1:59
    版主