简体   繁体   中英

How to access textblock or UI Element in static class and set the properties

How Can I access a textblock or textBox or rectangle box or UI Elements in XAML page from another static class or a static class helper to do tasks.

I have this problem:
a textBlock in a XAML page:

1) How to access this textblock in a static class to set Foreground color for textblock or set background Rectangle box or other UI element thru a static Class:

2) How to pass the textBlock to the static class and set it as below

textBlock.Foreground = Brushes.Navy;

Thanks

Though what you ask is really not a good idea in general it can be done (but again there are much better ways).

So basically you can assign the Dispatcher and TextBox to some field or property in the static class in the page OnNavigatedTo method.

You need to assign both because you can access the TextBox only from UI thread and you can invoke it by Dispatcher.RunAsync method.

EDIT:

<TextBox Name="textBox"/>

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    myStaticClass.TextBox = textBox;
    myStaticClass.Dispatcher = Dispatcher;
}

You can use Binding:

Xaml:

<TextBlock x:Name="AppStatusValueTextBlock" HorizontalAlignment="Left" Margin="1035,174,0,0" TextWrapping="Wrap" Text="{Binding ChangeTextBlockText}" VerticalAlignment="Top" Height="30" Width="230"/>

C#-Class:

class YourClass : INotifyPropertyChanged
{

 private string _changeTextBlockText;

 public string ChangeTextBlockText{
                get
                {
                    return _changeTextBlockText;
                }
                set
                {
                    _changeTextBlockText= value;
                    OnPropertyChanged();
                }
        }


 public event PropertyChangedEventHandler PropertyChanged;

 protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
 {
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
 }

}

EDIT: Example to change the foreground color

Xaml:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock x:Name="textBlockHex" HorizontalAlignment="Left" Margin="90,180,0,0" TextWrapping="Wrap" Text="hello" VerticalAlignment="Top" Height="75" Width="170" Foreground="{Binding TextBlockColorInHex}"/>
</Grid>

ViewModel:

class ViewModel : INotifyPropertyChanged
{

    public string TextBlockColorInHex
    {
        get
        {
            return _textBlockColorInHex;
        }
        set
        {
            this._textBlockColorInHex = value;
            this.OnPropertyChanged();
        }
    }

    public ViewModel()
    {
       SetColorFromThisClass("#ff0000");
    }

    private void SetColorFromThisClass(string colorInHex)
    {
        TextBlockColorInHex = colorInHex;
    }
    private string _textBlockColorInHex;


    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

MainPage.cs:

    public MainPage()
    {
        this.InitializeComponent();

        ViewModel daViewModel = new ViewModel();
        DataContext = daViewModel;

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM