WPF :: DataContext 在指向 STATIC class 时抛出 "Not Set To An Instance"
WPF :: DataContext Is throwing "Not Set To An Instance" When Pointing to a STATIC class
我正在按照 this Microsoft post 中的指导设置 Ribbon
具有静态属性的控件作为其 DataContext
以将 UI 绑定到数据模型。
相关的 XAML 看起来是这样的:
<Ribbon>
<RibbonGroup Header="Group1">
<RibbonToggleButton DataContext="{x:Static vm:WordModel.Bold}"/>
</RibbonGroup>
</Ribbon>
... 以及 DataContext
的 class:
public static class WordModel
{
private static readonly object LockObject = new object();
private static readonly Dictionary<string, ControlData> _dataCollection =
new Dictionary<string, ControlData>();
public static ControlData Bold
{
get
{
lock (LockObject)
{
const string key = "Bold";
if (!_dataCollection.ContainsKey(key))
{
var data = new ToggleButtonData()
{
Command = EditingCommands.ToggleBold,
IsChecked = false,
KeyTip = "B",
SmallImage = Application.Current.Resources["BoldIcon"] as DrawingImage,
ToolTipDescription = "Toggles the Bold font weight on the current selection.",
ToolTipTitle = "Bold (Ctrl + B)",
};
_dataCollection[key] = data;
}
return _dataCollection[key];
}
}
}
}
... 这是一个 static class 和 属性。为什么编译器会给我关于“Object reference not set to an instance of an object
”的蓝色波浪线和抱怨?我将 DataContext 设置为指向带有 {x:Static ...}
位的 static 引用,对吗?
我敢肯定我在这里遗漏了一些简单的东西,但如果我知道它是什么就该死了。
这似乎是设计时错误。
每当设计人员在设计时访问事物时,可能会触及流程中对象的构造函数和属性。如果您在那段时间不想解决它的项目,请检查以在设计时解决它。
WPF
if (!DesignerProperties.IsInDesignModeProperty)
银光
if (!DesignerProperties.IsInDesignTool)
由于 "thou shouldst reconsider deleting answered questions" 的推荐,我在这里发布答案并公开接受我的耻辱。 :-)
问题出在 setter 的 SmallImage:
private ImageSource _smallImage ;
public ImageSource SmallImage
{
get
{
return _smallImage;
}
set
{
if (_smallImage.Equals(value)) return;
_smallImage = value;
NotifyPropertyChanged(() => SmallImage);
}
}
正如您所期望的那样,_smallImage 最初是 null
,显然 null.Equals(value)
效果不佳。我将该行更改为
if (_smallImage == value) return;
世界一切都好。
我正在按照 this Microsoft post 中的指导设置 Ribbon
具有静态属性的控件作为其 DataContext
以将 UI 绑定到数据模型。
相关的 XAML 看起来是这样的:
<Ribbon>
<RibbonGroup Header="Group1">
<RibbonToggleButton DataContext="{x:Static vm:WordModel.Bold}"/>
</RibbonGroup>
</Ribbon>
... 以及 DataContext
的 class:
public static class WordModel
{
private static readonly object LockObject = new object();
private static readonly Dictionary<string, ControlData> _dataCollection =
new Dictionary<string, ControlData>();
public static ControlData Bold
{
get
{
lock (LockObject)
{
const string key = "Bold";
if (!_dataCollection.ContainsKey(key))
{
var data = new ToggleButtonData()
{
Command = EditingCommands.ToggleBold,
IsChecked = false,
KeyTip = "B",
SmallImage = Application.Current.Resources["BoldIcon"] as DrawingImage,
ToolTipDescription = "Toggles the Bold font weight on the current selection.",
ToolTipTitle = "Bold (Ctrl + B)",
};
_dataCollection[key] = data;
}
return _dataCollection[key];
}
}
}
}
... 这是一个 static class 和 属性。为什么编译器会给我关于“Object reference not set to an instance of an object
”的蓝色波浪线和抱怨?我将 DataContext 设置为指向带有 {x:Static ...}
位的 static 引用,对吗?
我敢肯定我在这里遗漏了一些简单的东西,但如果我知道它是什么就该死了。
这似乎是设计时错误。
每当设计人员在设计时访问事物时,可能会触及流程中对象的构造函数和属性。如果您在那段时间不想解决它的项目,请检查以在设计时解决它。
WPF
if (!DesignerProperties.IsInDesignModeProperty)
银光
if (!DesignerProperties.IsInDesignTool)
由于 "thou shouldst reconsider deleting answered questions" 的推荐,我在这里发布答案并公开接受我的耻辱。 :-)
问题出在 setter 的 SmallImage:
private ImageSource _smallImage ;
public ImageSource SmallImage
{
get
{
return _smallImage;
}
set
{
if (_smallImage.Equals(value)) return;
_smallImage = value;
NotifyPropertyChanged(() => SmallImage);
}
}
正如您所期望的那样,_smallImage 最初是 null
,显然 null.Equals(value)
效果不佳。我将该行更改为
if (_smallImage == value) return;
世界一切都好。