如何为本地属性创建资源?
在我的 WPF UserControl 中,我有一个这样定义的属性:
private Converters.CurrencyConverter _CurConverter = null;
public Converters.CurrencyConverter CurConverter
{
get
{
if (_CurConverter == null)
_CurConverter = new Converters.CurrencyConverter(decPlaces);
return _CurConverter;
}
}
我想在我的 xaml 中为其设置资源,但我不确定如何执行此操作。我已经尝试过:
<UserControl.Resources>
<ObjectDataProvider ObjectType="{x:Type LocalConverters:CurrencyConverter}" ObjectInstance="{CurConverter}" x:Key="LocalCurConverter" />
</UserControl.Resources>
但这显然是不对的,并且它给出了如何使用 ObjectInstance 的问题。我还尝试在资源声明中创建对象,如下所示:
<UserControl.Resources>
<LocalConverters:CurrencyConverter x:Key="CurConverter" />
</UserControl.Resources>
但这不起作用,因为我没有无参数构造函数。忘记这是一个转换器这一事实,如何在后面的代码中为属性创建资源?
谢谢
In my WPF UserControl, I've got a property defined as such:
private Converters.CurrencyConverter _CurConverter = null;
public Converters.CurrencyConverter CurConverter
{
get
{
if (_CurConverter == null)
_CurConverter = new Converters.CurrencyConverter(decPlaces);
return _CurConverter;
}
}
I want to set a resource to it in my xaml, but I'm not sure how to do it. I've tried this:
<UserControl.Resources>
<ObjectDataProvider ObjectType="{x:Type LocalConverters:CurrencyConverter}" ObjectInstance="{CurConverter}" x:Key="LocalCurConverter" />
</UserControl.Resources>
But that's obviously not right and it gives issues with how ObjectInstance is used. I've also tried to instead just create the object in the resource declaration as such:
<UserControl.Resources>
<LocalConverters:CurrencyConverter x:Key="CurConverter" />
</UserControl.Resources>
But that doesn't work because I don't have a parameterless constructor. Forgetting about the fact that this is a converter, how can I create a resource to a property in the code behind?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用了第二种方法,添加了一个无参数构造函数,然后在代码隐藏中设置原始构造函数接受的参数。
I used the second method, added a parameterless constructor, and then set the parameters the original constructor accepted in the code-behind.