如何从 XAML 正确引用类
好吧,这是一个超级超级菜鸟的问题,我几乎不好意思问...
我想在我的 XAML 文件中引用一个类。它是一个 DataTemplateSelector,用于为 DataGrid 列选择正确的编辑模板。
无论如何,我已经将该类写入后面的代码中,将本地命名空间添加到 XAML 的顶部,但是当我尝试从 XAML 引用该类时,它告诉我该类不存在于本地命名空间中。我一定错过了一些非常非常简单的东西,但我就是无法理解它......
这是我的代码。
XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:local="clr-namespace:CustomFields"
xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Class="CustomFields.MainWindow"
x:Name="Window"
Title="Define Custom Fields"
Width="425" Height="400" MinWidth="425" MinHeight="400">
<Window.Resources>
<ResourceDictionary>
<local:RangeValuesEditTemplateSelector>
blah blah blah...
</local:RangeValuesEditTemplateSelector>
</ResourceDictionary>
</Window.Resources>
C#:
namespace CustomFields
{
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
// Insert code required on object creation below this point.
}
}
public class RangeValuesEditTemplateSelector : DataTemplateSelector
{
public RangeValuesEditTemplateSelector(){
MessageBox.Show("hello");
}
}
}
知道我做错了什么吗?我认为这应该像 1-2-3 一样简单......
谢谢!
OK, this is a super super noob question, one that I'm almost embarrassed to ask...
I want to reference a class in my XAML file. It's a DataTemplateSelector for selecting the right edit template for a DataGrid column.
Anyway, I've written the class into my code behind, added the local namespace to the top of top of the XAML, but when I try to reference the class from the XAML, it tells me the class does not exist in the local namespace. I must be missing something really really simple but I just can't understand it...
Here's my code.
XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:local="clr-namespace:CustomFields"
xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Class="CustomFields.MainWindow"
x:Name="Window"
Title="Define Custom Fields"
Width="425" Height="400" MinWidth="425" MinHeight="400">
<Window.Resources>
<ResourceDictionary>
<local:RangeValuesEditTemplateSelector>
blah blah blah...
</local:RangeValuesEditTemplateSelector>
</ResourceDictionary>
</Window.Resources>
C#:
namespace CustomFields
{
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
// Insert code required on object creation below this point.
}
}
public class RangeValuesEditTemplateSelector : DataTemplateSelector
{
public RangeValuesEditTemplateSelector(){
MessageBox.Show("hello");
}
}
}
Any ideas what I'm doing wrong? I thought this should be simple as 1-2-3...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧...它突然开始工作了。只得重建。
OK... it suddenly started working. Just had to rebuild.
您可以添加一个键,以便可以在 xaml 中设置数据上下文,而不是在代码后面设置:
然后例如设置外部网格的 DataContext:
然后,该网格内的任何内容都可以直接绑定到您在代码后面设置的属性。
不确定这是否有用,只是想分享:)
You can add a key so you can set the datacontext in the xaml instead behind code:
Then for example set the outer grid's DataContext:
Then anything within that grid you can just bind directly to the property you set behind code.
Not sure if this is useful or not, just thought I'd share :)