C# 在代码隐藏中使用StaticResource和x:Static
我喜欢: 将抛出: WindowsBase.dll中发生“System.Windows.ResourceReferenceKeyNotFoundException”类型的异常,但未在用户代码中处理 其他信息:未找到“ToolBar.ButtonStyleKey”资源C# 在代码隐藏中使用StaticResource和x:Static,c#,wpf,code-behind,markup-extensions,C#,Wpf,Code Behind,Markup Extensions,我喜欢: 将抛出: WindowsBase.dll中发生“System.Windows.ResourceReferenceKeyNotFoundException”类型的异常,但未在用户代码中处理 其他信息:未找到“ToolBar.ButtonStyleKey”资源 根据您的工作代码,它应该如下所示: Style = (Style)Application.Current.FindResource(ToolBar.ButtonStyleKey) 换句话说,抛开引用。ButtonStyleKey不
根据您的工作代码,它应该如下所示:
Style = (Style)Application.Current.FindResource(ToolBar.ButtonStyleKey)
换句话说,抛开引用。ButtonStyleKey不是名称,它是返回具有正确名称的字符串的静态属性
var button = new Button
{
Style = (Style)Application.Current.FindResource("ToolBar.ButtonStyleKey"), // wrong
BorderThickness = new Thickness(0),
...
};
Style = (Style)Application.Current.FindResource(ToolBar.ButtonStyleKey)