我有点问题。我正在使用户能够选择一个文本块的大小,通过这些文本块,他可以通过执行其他操作来显示文本。 我的问题是我必须在文本块中添加一个边框,以向用户显示它有多大。 当我应用以下代码时,我的程序在这种情况下崩溃了:
//create a TextBlock at desired position
TextBlock tmpTextBlock = new TextBlock
{
Width = 166,
Height = Math.Max(tmpY1, tmpY2) - Math.Min(tmpY1, tmpY2),
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(0, Math.Min(tmpY1, tmpY2) - 50, 0, (int)WeekGrid.ActualHeight - Math.Max(tmpY1, tmpY2)),
Text = "Type stuff here"
};
tmpTextBlock.Holding += tmpTextBox_Holding;
tmpTextBlock.RightTapped += tmpTextBox_RightTapped;
WeekGrid.Children.Add(tmpTextBlock);
Grid.SetRow(tmpTextBlock, 1);
//add the border - these lines produce the problem
Border border = new Border
{
Child = tmpTextBlock,
Background = this.Resources["ApplicationPageBackgroundThemeBrush"] as SolidColorBrush,
BorderBrush = this.Resources["ApplicationForegroundThemeBrush"] as SolidColorBrush,
BorderThickness = new Thickness(1),
};
以下异常是一个参数异常:
价值不在预期范围内。
修改
哎呀我已经解决了这个问题。我不得不删除将Textblock添加到网格中。 我现在遇到的问题是边框出现在网格周围 - 而不是文本块周围!
以下代码使这成为可能:
Border border = new Border
{
Child = tmpTextBlock,
Background = this.Resources["ApplicationPageBackgroundThemeBrush"] as SolidColorBrush,
BorderBrush = this.Resources["ApplicationForegroundThemeBrush"] as SolidColorBrush,
BorderThickness = new Thickness(1),
Padding = new Thickness(24)
};
WeekGrid.Children.Add(border);
Grid.SetRow(border, 1);
编辑2
问题又解决了。 我当然不得不删除文本块的边距设置!
非常感谢!
问候, FunkyPeanut
答案 0 :(得分:0)
你可以发布例外吗?
显然您的代码中存在错误:
WeekGrid.Children.Add(tmpTextBlock);
Grid.SetRow(tmpTextBlock, 1);
//add the border - these lines produce the problem
Border border = new Border
{
Child = tmpTextBlock,
Background = this.Resources["ApplicationPageBackgroundThemeBrush"] as SolidColorBrush,
BorderBrush = this.Resources["ApplicationForegroundThemeBrush"] as SolidColorBrush,
BorderThickness = new Thickness(1),
};
组件Border
就像一个“容器”,它接受一个元素,你应该切换到:
Border border = new Border
{
Child = tmpTextBlock,
Background = this.Resources["ApplicationPageBackgroundThemeBrush"] as SolidColorBrush,
BorderBrush = this.Resources["ApplicationForegroundThemeBrush"] as SolidColorBrush,
BorderThickness = new Thickness(1),
};
WeekGrid.Children.Add(border);
Grid.SetRow(border, 1);
您还应该检查资源是否“可用”以供您访问。