XAML 元素的布局度量覆盖不应返回PositiveInfinity作为其DesiredSize,即使传入Infinity作为可用大小

2guxujil  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(154)

因此,我试图实现一个简单的字距微调UserControl,以便与ListBoxTemplate中的DataBound文本一起使用,但我遇到了问题标题中的错误。
在VS或Expression Blend中进行开发时,我使用设计时数据填充数据,但我不确定这是否是原因,因为它是在生成时生成的,并且只有在填充数据时才会崩溃。
第一个

hfyxw5xn

hfyxw5xn1#

明白了...在用户控件中你要加

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

相关问题