【问题标题】:Using StaticResource and x:Static in code behind在后面的代码中使用 StaticResource 和 x:Static
【发布时间】:2017-12-27 15:33:31
【问题描述】:

我喜欢this flat button style

<Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" BorderThickness="0" ... />

尝试在后面的代码中创建这样的按钮:

var button = new Button
{
    Style = (Style)Application.Current.FindResource("ToolBar.ButtonStyleKey"), // wrong
    BorderThickness = new Thickness(0),
    ...
};

会抛出:

“System.Windows.ResourceReferenceKeyNotFoundException”类型的异常发生在 WindowsBase.dll 中,但未在用户代码中处理

附加信息:未找到“ToolBar.ButtonStyleKey”资源。

【问题讨论】:

    标签: c# wpf code-behind markup-extensions


    【解决方案1】:

    根据您的工作代码,它应该如下所示:

    Style = (Style)Application.Current.FindResource(ToolBar.ButtonStyleKey)
    

    换句话说,放弃引号。 ButtonStyleKey 不是名称,而是返回具有正确名称的字符串的静态属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-24
      • 2010-11-27
      • 2015-09-18
      • 2013-08-21
      • 2011-08-28
      • 1970-01-01
      • 1970-01-01
      • 2014-08-05
      相关资源
      最近更新 更多