Update text in adorner on button click

942 views Asked by At

I have created my custom adorner to cover my main window with a gray canvas alongwith a textblock at center to show some status text while i was working on other window.

What i am currently doing is fetching the required adornerElement(ie Canvas with a textblock) from my resources and passing it to an adorner in my view constructor like this -

 ResourceDictionary reportResourceDictionary = App.LoadComponent(new Uri("Resources/ReportResources.xaml", UriKind.Relative)) as ResourceDictionary;
 UIElement adornerElement = reportResourceDictionary["RefreshingReportAdorner"] as UIElement;
 mainWindowBlockMessageAdorner = new MainWindowBlockMessageAdorner(mainPanel, adornerElement);

But i want to update that text in textblock in some scenarios say if i click on some button in other window but how to update the text dynamically??

Adorner element from Resource file-

<Grid x:Key="RefreshingReportAdorner">
        <Rectangle Fill="Gray"
                   StrokeThickness="1"
                   Stroke="Gray"
                   HorizontalAlignment="Stretch"
                   VerticalAlignment="Stretch"/>
        <Border BorderBrush="Black"
                BorderThickness="2"
                Background="White"
                HorizontalAlignment="Center"
                VerticalAlignment="Center">
            <TextBlock i18n:LanguageManager.VisualId="6"
                       Text="Some Text(Update dynamically)"                       
                       Padding="15,10,15,10"/>
        </Border>
    </Grid>

Let me know if additional code or approach required..

1

There are 1 answers

0
Alex Zhevzhik On BEST ANSWER

Have you tried to create some model and push it to RefreshingReportAdorner element's DataContext?

Code:

 var reportResourceDictionary = App.LoadComponent(new Uri("Resources/ReportResources.xaml", UriKind.Relative)) as ResourceDictionary;
 var adornerElement = reportResourceDictionary["RefreshingReportAdorner"] as FrameworkElement;
 var model = new Model(); 
 model.MyText = "Initial text";
 adornerElement.DataContext = model;
 mainWindowBlockMessageAdorner = new MainWindowBlockMessageAdorner(mainPanel, adornerElement);
 ... 
 model.MyText = "Text after click";

XAML:

        <TextBlock i18n:LanguageManager.VisualId="6"
                   Text="{Binding MyText}"                       
                   Padding="15,10,15,10"/>

Model:

public class Item : INotifyPropertyChanged
{
    private string _myText;
    public string MyText
    {
        get
        {
            return this._myText;
        }
        set
        {
            this._myText= value;
            this.OnPropertyChanged("MyText");
        }
    }
}