Search code examples
c#xamlwindows-phone-8windows-phonesilverlight-toolkit

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 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:

<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?

It says myTextBlock doesn't exist in the namespace.

It does work in WinRT apps, but not in Silverlight...


Solution

  • I would recommend you Databind that TextBlock.Text instead. 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().

    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


    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:

    <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. The trick is when you're dealing with a DataTemplate, the DataContext is actually set in 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