[英]WP8 silverlight: can't find a textblock in a DataTemplate
I know this question must be already asked, but I can't find any useful answer. 我知道这个问题必须已经问过,但我找不到任何有用的答案。
I have a toolkit's CustomMessageBox, very similar to this Microsoft example: 我有一个工具包的CustomMessageBox,非常类似于这个Microsoft示例:
CustomMessageBox messageBox = new CustomMessageBox()
{
ContentTemplate = (DataTemplate)this.Resources["myContentTemplate"],
LeftButtonContent = "speak",
RightButtonContent = "read it",
IsFullScreen = false
};
messageBox.Dismissed += (s1, e1) =>
{
switch (e1.Result)
{
case CustomMessageBoxResult.LeftButton:
// Do something.
break;
case CustomMessageBoxResult.RightButton:
// Do something.
break;
case CustomMessageBoxResult.None:
// Do something.
break;
default:
break;
}
};
messageBox.Show();
The messagebox content is defined in a DataTemplate, to semplify: 消息框内容在DataTemplate中定义,以简化:
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="myContentTemplate">
<TextBlock x:Name="myTextBlock" />
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
How can I set the myTextBlock.Text
via code-behind? 如何通过代码隐藏设置myTextBlock.Text
?
It says myTextBlock
doesn't exist in the namespace. 它说myTextBlock
在命名空间中不存在。
It does work in WinRT apps, but not in Silverlight... 它在WinRT应用程序中有效,但在Silverlight中无效...
I would recommend you Databind that TextBlock.Text instead. 我建议您使用TextBlock.Text作为Databind。 But if you really want to traverse that object. 但如果你真的想要遍历那个对象。 You need to the derived a custom messagebox from that one and on the OnApplyTemplate you can get the children pretty easy using GetTemplateChild(). 您需要从该组件派生自定义消息框,并且在OnApplyTemplate上,您可以使用GetTemplateChild()轻松获得子项。
But if you want to do it the hard way: You can use the VisualTreeHelper, matter of fact someone already wrote you a nice traversing function: FindVisualChildByName 但是如果你想这么做的话:你可以使用VisualTreeHelper,事实上有人已经给你写了一个很好的遍历函数: FindVisualChildByName
Solution to your binding issues 解决您的绑定问题
Sample Binding Class 样本绑定类
public class MainViewModel : INotifyPropertyChanged
{
private string _messagebox_text = "";
public string messagebox_text
{
get
{
return _messagebox_text;
}
set
{
_messagebox_text = value;
NotifyPropertyChanged("messagebox_text");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Then your DataTemplate gets change to: 然后你的DataTemplate变为:
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="myContentTemplate">
<TextBlock x:Name="myTextBlock" Text="{Binding messagebox_text}" />
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
Then you need to set your DataContext correctly. 然后,您需要正确设置DataContext。 The trick is when you're dealing with a DataTemplate, the DataContext is actually set in Content 诀窍是当你处理DataTemplate时,DataContext实际上是在Content中设置的
So putting it all together 所以把它们放在一起
MainViewModel _mvm = new MainViewModel();
_mvm.messagebox_text = "what ever";
messageBox = new CustomMessageBox()
{
ContentTemplate = (DataTemplate)this.Resources["myContentTemplate"],
LeftButtonContent = "speak",
RightButtonContent = "read it",
IsFullScreen = false
};
messageBox.Content = _mvm; // set the bind
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.