0

我正在使用 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;
            }
        }
    }
}
4

1 回答 1

0

您正在错误地进行绑定。你应该做 bindingLiveTileItemsSourceas ObservableCollection<Tile> TileData。此外,DataContext将传递给LiveTile自身而不是其模板。查看下面的代码。

XAML

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

C#

public class Tile
{
    public string Msg { get; set; }
    public Uri Image { get; set; }
}

public class LiveTileData
{
    public LiveTileData()
    {
        tileData.Add(new Tile { Msg = "Stack Overflow", Image = new Uri("http://jenswinter.com/image.axd?picture=stackoverflow-logo-250.png") });
        tileData.Add(new Tile { Msg = "Super User", Image = new Uri("http://superuser.com/content/superuser/img/logo.png") });
    }
    private static ObservableCollection<Tile> tileData = new ObservableCollection<Tile>();
    public static ObservableCollection<Tile> TileData
    {
        get
        {
            return tileData;
        }
    }
}
于 2013-09-17T09:58:01.730 回答