Windows Phone 7 Silverlight DataTemplate HyperlinkBut​​ton 内容

发布于 2024-10-21 17:50:45 字数 2378 浏览 4 评论 0原文

我正在尝试创建一个由外部数据填充的列表,并且我有一个数据模板。
列表的每个元素都由一个图像和两个文本块组成,并链接到特定页面。

我正在尝试这样做,但是当我用 HyperlinkBut​​ton 包装结构时,我只得到一个空白页面。

我不知道我是否做了一些愚蠢的错误,或者超链接按钮中不可能有这么多项目。如果不可能这样做,有人可以指导我找到最佳解决方案吗?代码如下:

<Grid x:Name="ContentPanel" Margin="0,140,0,0" Background="White">
    <ListBox HorizontalAlignment="Stretch" Name="itemList" VerticalContentAlignment="Stretch">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <HyperlinkButton>
                    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinWidth="480">
                        <Grid.Background>
                            <ImageBrush ImageSource="/app;component/Images/degradat_cela.png" Stretch="UniformToFill" AlignmentY="Top" AlignmentX="Left" />
                        </Grid.Background>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="35" />
                            <RowDefinition Height="35" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="50" />
                            <ColumnDefinition Width="430*" />
                        </Grid.ColumnDefinitions>

                        <Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="38" Width="38" Grid.RowSpan="2" Grid.Column="0" Grid.Row="0" BorderThickness="1" BorderBrush="#FFFF003F" Padding="1">
                            <Image HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="listImage" Width="36" Height="36" Source="{Binding image}" />
                        </Border>
                        <TextBlock Margin="5 12 0 0" Grid.Column="1" Grid.Row="0" Name="title" Foreground="Black" Text="{Binding title}" FontWeight="Bold" FontSize="18" />
                        <TextBlock Margin="5 0 0 8" Grid.Column="1" Grid.Row="1" Name="description" Foreground="Black" Text="{Binding subtitle}" FontSize="14" />
                    </Grid>
                </HyperlinkButton>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

我还将接受任何使我的代码变得更好的建议,因为我是 .NET 的新手,而且我可能没有以最好的方式做事!

I'm trying to make a list that is populated by external data and I have a datatemplate for it.
Each element of the list is composed of an image and two textblocks, and is linked to a specific page.

I'm trying to do this but when I wrap the structure with an HyperlinkButton, I just get a blank page.

I don't know if I'm doing something stupid wrong or it's not possible to have so many items in an HyperlinkButton. If it's not possible to do it this way, can someone guide me to the best solution to do this? Here's the code:

<Grid x:Name="ContentPanel" Margin="0,140,0,0" Background="White">
    <ListBox HorizontalAlignment="Stretch" Name="itemList" VerticalContentAlignment="Stretch">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <HyperlinkButton>
                    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinWidth="480">
                        <Grid.Background>
                            <ImageBrush ImageSource="/app;component/Images/degradat_cela.png" Stretch="UniformToFill" AlignmentY="Top" AlignmentX="Left" />
                        </Grid.Background>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="35" />
                            <RowDefinition Height="35" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="50" />
                            <ColumnDefinition Width="430*" />
                        </Grid.ColumnDefinitions>

                        <Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="38" Width="38" Grid.RowSpan="2" Grid.Column="0" Grid.Row="0" BorderThickness="1" BorderBrush="#FFFF003F" Padding="1">
                            <Image HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="listImage" Width="36" Height="36" Source="{Binding image}" />
                        </Border>
                        <TextBlock Margin="5 12 0 0" Grid.Column="1" Grid.Row="0" Name="title" Foreground="Black" Text="{Binding title}" FontWeight="Bold" FontSize="18" />
                        <TextBlock Margin="5 0 0 8" Grid.Column="1" Grid.Row="1" Name="description" Foreground="Black" Text="{Binding subtitle}" FontSize="14" />
                    </Grid>
                </HyperlinkButton>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

I will also accept any suggestions to make my code better, as I'm new to .NET and I probably don't do things the best way!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

删除 HyperlinkBut​​ton 并使用 ListBox 的 SelectionChanged 事件。因此,将此属性添加到您的列表框:

SelectionChanged="myListBox_SelectionChanged"

然后在后面的代码中执行以下操作:

private void myListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    if ((sender as ListBox).SelectedIndex == -1)
        return;

    NavigationService.Navigate(new System.Uri(string.Format("/Drilldown.xaml?Index={0}",(sender as ListBox).SelectedIndex),System.UriKind.Relative));
}

此代码假定一个深入页面使用查询字符串来更改其布局。这只是举例。如果您想引用绑定项的某些属性而不是索引,则可以执行类似以下操作(假设 ID 属性):

int itemID = ((sender as ListBox).SelectedItem as MyApp.Model.myItem).ID;
NavigationService.Navigate(new System.Uri(string.Format("/Drilldown.xaml?ID={0}",itemID),System.UriKind.Relative));

Remove the HyperlinkButton and instead use the SelectionChanged event of the ListBox. So add this property to your ListBox:

SelectionChanged="myListBox_SelectionChanged"

Then in your code behind do this:

private void myListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    if ((sender as ListBox).SelectedIndex == -1)
        return;

    NavigationService.Navigate(new System.Uri(string.Format("/Drilldown.xaml?Index={0}",(sender as ListBox).SelectedIndex),System.UriKind.Relative));
}

This code assumes a drilldown page that uses a query string to change it's layout. This is just for example. If instead of index you wanted to reference some property of the bound item you could instead do something like this (assuming an ID property):

int itemID = ((sender as ListBox).SelectedItem as MyApp.Model.myItem).ID;
NavigationService.Navigate(new System.Uri(string.Format("/Drilldown.xaml?ID={0}",itemID),System.UriKind.Relative));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文