1

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

the_nuts
  • 5,634
  • 1
  • 36
  • 68

1 Answers1

3

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
Community
  • 1
  • 1
Chubosaurus Software
  • 8,133
  • 2
  • 20
  • 26
  • I've spent hours trying to bind it to a local variable... but it doesn't work. If I put the anywhere in the page it shows the score, but in that messagebox, it shows nothing... – the_nuts Aug 24 '14 at 20:09
  • Yeah I understand your frustration. I modify the solution to add in how you Databind it correctly. – Chubosaurus Software Aug 25 '14 at 07:11