为什么我看不到PNG文件中的列表框?

时间:2012-06-21 15:50:28

标签: c# silverlight windows-phone-7 listbox live-tile

我想要什么
我想在WP7.5中显示一个自定义的实时图块,一个文本块和一个列表框,其中包含4个字符串作为其列表项。所以我创建了一个包含所有内容的图像,并将其显示为实时图块的背景。

我的代码

        //prepare the background
        WriteableBitmap bmp = new WriteableBitmap(173, 173);
        BitmapImage logo = new BitmapImage(new Uri("/images/tile/SecondaryTile.png", UriKind.Relative));            
        Image img = new Image { Source = logo };

        // Force the bitmapimage to load it's properties so the transform will work
        logo.CreateOptions = BitmapCreateOptions.None;

        //create a textblock
        TextBlock taskNumberShow = new TextBlock();
        taskNumberShow.Foreground = new SolidColorBrush(Colors.White);
        taskNumberShow.FontSize = 35;
        taskNumberShow.HorizontalAlignment = HorizontalAlignment.Center;
        taskNumberShow.FontFamily = (FontFamily)Application.Current.Resources["PhoneFontFamilySemiBold"];
        taskNumberShow.Text = "35";

        //define the position
        TranslateTransform tt1 = new TranslateTransform();
        tt1.X = 45;
        tt1.Y = 7;
        bmp.Render(taskNumberShow, tt1);

        //create a listbox
        ListBox listBox1 = new ListBox();
        listBox1.Height = 100;
        listBox1.Width = 173;
        listBox1.HorizontalAlignment = HorizontalAlignment.Center;
        //listBox1.VerticalAlignment = VerticalAlignment.Bottom;
        //listBox1.Margin = new Thickness(50, 150, 0, 0);

        //put a string array in the listbox, the length of this array is 4
        for (int i=0; i < TaskList.Length; i++)
        {
            //先生成一堆textBlock
            TextBlock txtBlk = new TextBlock();
            txtBlk.Name = "txtBlk" + i.ToString();
            txtBlk.Text = TaskList[i];
            txtBlk.FontSize = 17;
            txtBlk.Width = 173;
            txtBlk.TextWrapping = TextWrapping.NoWrap;
            txtBlk.Foreground = new SolidColorBrush(Colors.White);
            listBox1.Items.Add(txtBlk);
        }

        //define the position
        tt1.X = 0;
        tt1.Y = 40;
        bmp.Render(listBox1, tt1);


        //add the backgroundpic
        tt1.X = 0;
        tt1.Y = 0;
        bmp.Render(img, tt1);
        bmp.Invalidate();

        ExtendedImage extendImage = bmp.ToImage();

什么是END
我可以看到文本块,但我看不到列表框。我尝试使用下面的代码直接将字符串数组的项目(TaskList [])分配到列表框中

for (int i=0, i<TaskList.Length, i++)
{
 Listbox1.Items.Add(TaskList[i]);
}

但结果是我也看不到它。

如何解决此问题?为什么我看不到图片中的列表框?

1 个答案:

答案 0 :(得分:0)

这可能与ListBox的虚拟性质有关。一种选择是将ItemsPanel设置为StackPanel。另一种可能更简单的方法就是使用StackPanel

//create a StackPanel
    StackPanel stackPanel1 = new StackPanel();
    stackPanel1.Height = 100;
    stackPanel1.Width = 173;
    stackPanel1.HorizontalAlignment = HorizontalAlignment.Center;

    //put a string array in the listbox, the length of this array is 4
    for (int i=0; i < TaskList.Length; i++)
    {
        //先生成一堆textBlock
        TextBlock txtBlk = new TextBlock();
        txtBlk.Name = "txtBlk" + i.ToString();
        txtBlk.Text = TaskList[i];
        txtBlk.FontSize = 17;
        txtBlk.Width = 173;
        txtBlk.TextWrapping = TextWrapping.NoWrap;
        txtBlk.Foreground = new SolidColorBrush(Colors.White);
        stackPanel1.Children.Add.Add(txtBlk);
    }

我还建议将所有控件放入像Grid这样的父容器中,并将Grid渲染为一个整体。