silverlight c#net中的数据模板
我正在为listbox中的项目创建一个datatemplate,并使用silverlight c#net中的数据模板,c#,silverlight,windows-phone-7,C#,Silverlight,Windows Phone 7,我正在为listbox中的项目创建一个datatemplate,并使用 (DataTemplate)XamlReader.Load(template),其中template是 string template = String.Concat(@ "<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns
(DataTemplate)XamlReader.Load(template)
,其中template是
string template = String.Concat(@
"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<Border BorderBrush='#334873' BorderThickness='1,1,1,1' Width='450'>
<TextBox Height='72' HorizontalAlignment='Left' Margin='10,10,0,0' TextChanged="OnTextChanged" VerticalAlignment="Top" Width="460" />"
...................
"</Border></DataTemplate>");
string模板=string.Concat(@
"
"
...................
"");
由于“OnTextChanged”事件注册表项,我收到错误消息
我想在模板代码中注册一个事件
如何做?给您一个答案:当您使用模板时,以这种方式映射事件是相当困难的,因为模板不知道它放在哪里。所以
OnTextChanged
在这种情况下并不意味着什么
<>你应该考虑绑定。当您准备好使用DataTemplate时,通常的情况是将TextBox文本属性绑定到某个模型属性
<TextBox Height='72' HorizontalAlignment='Left' Margin='10,10,0,0' Text="{Binding MyTextProperty}" VerticalAlignment="Top" Width="460" />"
”
当然,这里的DataContext应该有一个属性
MyTextProperty
,但没有您的代码(您使用模板的地方)我不能提供更多细节。给你一个答案:当你使用一个模板时,以这种方式映射一个事件是相当困难的,因为模板不知道它放在哪里。因此OnTextChanged
在这种上下文中并不意味着什么
您应该考虑绑定。当您准备使用DATA模板时,通常的故事是将Text Box文本属性绑定到一些模型属性
。<TextBox Height='72' HorizontalAlignment='Left' Margin='10,10,0,0' Text="{Binding MyTextProperty}" VerticalAlignment="Top" Width="460" />"
”
当然,这里的DataContext应该有一个属性
MyTextProperty
,但没有您的代码(您使用模板的地方)我无法提供更多详细信息。请澄清您正在尝试连接的是哪种类型的文本,以及连接到哪种元素?在那里-他想将TextBox TextChanged映射到方法OnTextChanged…请澄清您正在尝试连接的是哪种类型的文本,以及连接到哪种元素?在那里-他想将TextBox TextChanged映射到方法ContextChanged…不,我处理的是其他一些需要注册这样一个事件的场景。IMHO最好是走MVVM路线,让ViewModel处理这些事情不,我处理的是其他一些需要注册这样一个事件的场景。IMHO最好是走MVVM路线,让ViewModel处理这些事情