簡體   English   中英

WP8 silverlight:在DataTemplate中找不到文本塊

[英]WP8 silverlight: can't find a textblock in a DataTemplate

我知道這個問題必須已經問過,但我找不到任何有用的答案。

我有一個工具包的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();

消息框內容在DataTemplate中定義,以簡化:

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="myContentTemplate">
        <TextBlock x:Name="myTextBlock" />
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>

如何通過代碼隱藏設置myTextBlock.Text

它說myTextBlock在命名空間中不存在。

它在WinRT應用程序中有效,但在Silverlight中無效...

我建議您使用TextBlock.Text作為Databind。 但如果你真的想要遍歷那個對象。 您需要從該組件派生自定義消息框,並且在OnApplyTemplate上,您可以使用GetTemplateChild()輕松獲得子項。

但是如果你想這么做的話:你可以使用VisualTreeHelper,事實上有人已經給你寫了一個很好的遍歷函數: FindVisualChildByName


解決您的綁定問題

樣本綁定類

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));
        }
    }
}

然后你的DataTemplate變為:

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="myContentTemplate">
        <TextBlock x:Name="myTextBlock" Text="{Binding messagebox_text}" />
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>

然后,您需要正確設置DataContext。 訣竅是當你處理DataTemplate時,DataContext實際上是在Content中設置的

所以把它們放在一起

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM