【问题标题】:Certificate and correct answer for the question证书和问题的正确答案
【发布时间】:2011-03-20 13:29:24
【问题描述】:

我正在准备考试和研究问题。但是我有一个问题,我认为答案是错误的。以下是正确答案为 D 的问题:

您使用 Microsoft .NET Framework 4 来 创建 Windows 演示文稿 基础 (WPF) 应用程序。这 应用程序有一个名为 具有 StackPanel 的 MainWindow 控件名为 sp 作为根元素。 你想创建一个 Button 控件 包含一个 TextBlock 控件 “保存”文本属性。你需要 动态创建控件并添加 sp的控制。哪个代码段 你应该写在构造函数中 MainWindow 类

答:

Button btn = new Button();
TextBlock text = new TextBlock();
text.Text = "Save";
btn.Content = text;
sp.DataContext = btn;

乙:

Button btn = new Button();
TextBlock text = new TextBlock();
text.Text = "Save";
btn.Content = text;
sp.Children.Add(btn);

C:

Button btn = new Button();
TextBlock text = new TextBlock();
text.Text = "Save";
sp.Children.Add(btn);
sp.Children.Add(text);

D:

Button btn = new Button();
TextBlock text = new TextBlock();
text.Text = "Save";
btn.ContentTemplateSelector.SelectTemplate(text, null);
sp.Children.Add(btn);

在我看来正确答案是B?你有什么建议吗?

【问题讨论】:

  • 我认为你是对的,B 是正确的答案。其他答案不正确。
  • 从未编写过 .NET 或 WPF,但我也会选择 B。似乎正确分配了两个对象的属性。

标签: wpf


【解决方案1】:

我认为你是对的。答案 D 完全没有意义,因为:

  1. 您不需要ContentTemplateSelector,因为您明确定义了内容
  2. ContentTemplateSelector 不应显式使用,它由 ContentControl 在需要呈现非可视内容时使用
  3. ContentTemplateSelector 默认为 null,因此答案 D 中的代码会因 NullReferenceException 而崩溃

【讨论】:

    【解决方案2】:

    我上周通过了同样的考试。我同意正确答案应该是 B。 您可以在示例应用程序中尝试这两种方法,您会发现 D 不起作用。

    【讨论】:

    • 嗨乔吉。你能告诉我考试怎么样吗?这些问题是否类似于练习测试所包含的内容?我已经检查过 D 没有工作
    • 我也有这个问题和其他问题。我以 982 分通过。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 2020-01-16
    相关资源
    最近更新 更多