0

我有一个小问题。我使用户能够选择文本块的大小,他可以在其中通过做一些其他的事情来显示文本。我的问题是我必须在文本块中添加一个边框以向用户显示它有多大。当我应用以下代码时,我的程序在这种情况下崩溃了:

                //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),
                };

后面的 Exception 是一个参数异常:

值不在预期范围内。

编辑

哎呀,我已经解决了这个问题。我不得不删除将 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

4

1 回答 1

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

您还应该检查资源是否“可用”供您访问。

于 2013-10-12T14:18:06.660 回答