Home > Net >  Binding TextBlock field to backend variable
Binding TextBlock field to backend variable

Time:05-31

First time really using WPF - thought I'd have a go at remaking something I did a while back in Java. I'm trying to bind the Text value of a TextBlock on a popup to something that gets set in the backend, so I can use one handler method to display any message on said popup. I've been trying multiple different routes, such as fully binding it in the cs instead of XAML like so:

<--XAML-->
<Popup Margin="89,75,0,0" Name="verif_popup" HorizontalAlignment="Left" VerticalAlignment="Top" IsOpen="False" PopupAnimation="Slide" Placement="Center" Width="100" Height="100" Grid.Column="1">
                            <Popup.Effect>
                                <BlurEffect/>
                            </Popup.Effect>
                            <Canvas Background="Azure">
                                <TextBlock Name="VerifTextBlock"/>
                            </Canvas>
                        </Popup>

<--CS-->
private void SmallPopupHandler(string text)
        {
            Binding binding = new("Text")
            {
                Source = text
            };
            VerifTextBlock.SetBinding(TextBlock.TextProperty, binding);
            verif_popup.IsOpen = true;
        }

But it doesn't like the fact that the string isn't a TextBlock property, I sort of knew this wouldn't work but it seems the most logical to me having come from swing. There also doesn't seem to be a way for me to cast it to it and im not in the mood for making my own dependency property rn... The next thing I tried was to bind the value to a field in the class, but I just got a stackoverflow error (haha nice)

<--XAML-->
<Popup Margin="89,75,0,0" Name="verif_popup" HorizontalAlignment="Left" VerticalAlignment="Top" IsOpen="False" PopupAnimation="Slide" Placement="Center" Width="100" Height="100" Grid.Column="1">
                            <Popup.Effect>
                                <BlurEffect/>
                            </Popup.Effect>
                            <Canvas Background="Azure">
                                <Canvas.DataContext>
                                    <local:MainWindow/>
                                </Canvas.DataContext>
                                <TextBlock Name="VerifTextBlock" Text="{Binding Popup_message}"/>
                            </Canvas>
                        </Popup>

<--CS-->
public partial class MainWindow : Window
    {

        public string? Popup_message { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }

I also tried making an interfacing class of sorts to see if that would work around the stackoverflow error (haha) but as im sure you could have guessed by now, that didn't work either...

Kinda pulling my hair out so any help would be greatly appreciated! Thanks in advance!

CodePudding user response:

You could just set the Text property of the VerifTextBlock directly as suggested by @Clemens:

private void SmallPopupHandler(string text)
{
    VerifTextBlock.Text = text;
    verif_popup.IsOpen = true;
}

If you really do want to use a binding for whatever reason, then remove the binding path. This should work:

private void SmallPopupHandler(string text)
{
    Binding binding = new()
    {
        Source = text
    };
    VerifTextBlock.SetBinding(TextBlock.TextProperty, binding);
    verif_popup.IsOpen = true;
}
  • Related