元素的布局测量覆盖不应将PositiveInfinity作为DesiredSize返回,即使Infinity作为可用大小传入也是如此

时间:2011-11-10 09:33:25

标签: c# silverlight xaml windows-phone-7

所以我试图在ListBoxTemplate中使用简单的Kerning UserControl与DataBound文本一起使用,并且我收到了此报告标题中的错误。我一直试图解决这个问题几个小时没有运气。我正在使用设计时数据填充数据,而我在VS或Expression Blend中进行开发,但我不确定这是否是因为它构建的原因并且仅在填充数据时崩溃。

<ListBox
            x:Name="MainList"
            ItemsSource="{Binding FeedItems}"
            SelectionChanged="ListBox_SelectionChanged"
            >
            <ListBox.ItemTemplate>

                <DataTemplate>
                    <StackPanel Orientation="Horizontal" x:Uid="{Binding ItemLink}" Margin="10">
                        <Controls:KerningTextBlock
                        Spacing="2"
                        Font="Verdana"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Left"
                        FontSize="32"
                        InputText="{Binding ItemTitle}"/>

                ....

    private StackPanel Stack = new StackPanel()
    {
        FlowDirection = System.Windows.FlowDirection.LeftToRight,
        Orientation = System.Windows.Controls.Orientation.Horizontal
    };

    private void KernIt()
    {
        // Clear the contents
        this.LayoutRoot.Children.Clear();

        // Convert input string to character array
        char[] Letters = !string.IsNullOrEmpty(this.InputText)? this.InputText.ToCharArray() : " ".ToCharArray();

        // For each item create a new text block with the following test
        foreach (var letter in Letters)
        {
            // Set up the formatted text block
            TextBlock TempText = new TextBlock();
            TempText.FontFamily = new FontFamily(this.Font);
            TempText.FontSize = 30;
            TempText.Padding = new Thickness(0, 0, this.Spacing, 0);
            TempText.Text = letter.ToString();

            // Add to the stack
            Stack.Children.Add(TempText);
        }
        // Add to the grid
        if (Stack.Children.Count() > 0)
            this.LayoutRoot.Children.Add(Stack);
    }

    public string InputText { get; set; }
    public double Spacing { get; set;}
    public string Font { get; set; }

1 个答案:

答案 0 :(得分:1)

搞定...在用户控件中你必须添加

DataContext="{Binding}"

然后设置布局根目录,因为我正在清除LayoutRoot(现在需要更改为SubRoot.Children.Clear():

<Grid x:Name="LayoutRoot" >
    <TextBlock x:Name="Title" Text="{Binding ItemTitle}"/>
    <Grid x:Name="SubRoot">
    </Grid>
</Grid>

接下来在代码中添加一个on loaded事件:

public KerningTextBlock()
{
    UpdateLayout();
    InitializeComponent();
    this.Loaded += new RoutedEventHandler(KerningTextBlock_Loaded);
}
 void KerningTextBlock_Loaded(object sender, RoutedEventArgs e)
{
     if (string.IsNullOrEmpty(Title.Text))
        this.InputText = "why am I empty?";
    else
        this.InputText = Title.Text;
    KernIt();
}

然后在您调用用户控件的位置将其更改为:

<Controls:KerningTextBlock
    DataContext="{Binding}"
    Spacing="5"
    Font="Verdana"
    x:Name="Button_Name"
    Margin="135,5,15,0"
    VerticalAlignment="Center"
    HorizontalAlignment="Left"
    FontSize="32"/>