Windows 8 Live Hub磁贴

时间:2013-09-16 11:34:27

标签: c# windows-8 windows-runtime microsoft-metro winrt-xaml

我正在使用Callisto Toolkit在Windows 8应用程序中创建实时中心磁贴,但我无法为我的实时磁贴执行数据绑定。我正在使用的代码如下:

<callisto:LiveTile x:Name="liveTile2" 
            Background="#3B5998"
            ItemsSource="{Binding Tile}"
            Grid.Column="5"
            Grid.Row="1"
            Height="250"
            VerticalAlignment="Top"
            BorderBrush="White" BorderThickness="1"
            Margin="5" 
            Direction="Up">
            <callisto:LiveTile.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="5">
                        <Grid.DataContext>
                            <ViewModel:Tile/>
                        </Grid.DataContext>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>

                        <Image x:Name="tileImage" Source="{Binding Image}"/>


                        <TextBlock Foreground="White"
                            TextWrapping="Wrap"
                            TextTrimming="WordEllipsis" 
                            FontSize="14"
                            Grid.Row="1" Margin="0,71,0,0" Text="{Binding Msg}" />



                    </Grid>
                </DataTemplate>
            </callisto:LiveTile.ItemTemplate>
            <callisto:LiveTile.DataContext>
                <ViewModel:LiveTileData/>
            </callisto:LiveTile.DataContext>
        </callisto:LiveTile>

这是瓷砖的类:

namespace Test.ViewModel
{
    public class Tile
    {
        public string Msg { get; set; }

        public Uri Image { get; set; }
    }

    public class LiveTileData
    {
        private static ObservableCollection<Tile> tileData = new ObservableCollection<Tile>();

        public static ObservableCollection<Tile> TileData
        {
            get
            {
                return tileData;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的绑定不正确。您应该将LiveTile的{​​{1}}绑定为ItemsSource。此外,ObservableCollection<Tile> TileData传递给DataContext本身而不是其模板。请查看以下代码。

XAML

LiveTile

C#

<callisto:LiveTile x:Name="liveTile2" 
    Background="#3B5998"
    ItemsSource="{Binding TileData}"
    Grid.Column="5"
    Grid.Row="1"
    Height="250"
    VerticalAlignment="Top"
    BorderBrush="White" BorderThickness="1"
    Margin="5" 
    Direction="Up">
    <callisto:LiveTile.ItemTemplate>
        <DataTemplate>
            <Grid Margin="5">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <Image x:Name="tileImage" Source="{Binding Image}" Stretch="None"/>

                <TextBlock Foreground="White"
                    TextWrapping="Wrap"
                    TextTrimming="WordEllipsis" 
                    FontSize="14"
                    Grid.Row="1" Margin="0,71,0,0" Text="{Binding Msg}" />

            </Grid>
        </DataTemplate>
    </callisto:LiveTile.ItemTemplate>
    <callisto:LiveTile.DataContext>
        <ViewModel:LiveTileData />
    </callisto:LiveTile.DataContext>
</callisto:LiveTile>