David G

David G

  • 1.5k
  • 224
  • 14.5k

WPF change UserControl textblock from another window

Feb 7 2018 10:23 AM

Hey all I am new at WPF so here is my question:

How can I change the text in a TextBlock from my mainWindow when the textBlock is in the window named curTemp.xaml?

curTemp.xaml code:

  1. public partial class curTemp : UserControl  
  2.     {  
  3.         public string _valTempChange  
  4.         {  
  5.             get { return middleForcastCurrentTemp.Text; }  
  6.             set { middleForcastCurrentTemp.Text = value; }  
  7.         }  
  8.   
  9.         public curTemp()  
  10.         {  
  11.             InitializeComponent();  
  12.         }  
  13.     }  

Xaml of the above UserControl:

  1. <Grid>  
  2.         <TextBlock TextWrapping="Wrap" Padding="5" Foreground="White" Panel.ZIndex="7" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="15,-110,-43,0" Width="198" Height="122">  
  3.             <TextBlock.Effect>  
  4.                 <DropShadowEffect BlurRadius="4" Direction="0" ShadowDepth="0" />  
  5.             </TextBlock.Effect>  
  6.                     <outlineText:OutlinedTextBlock Height="146" Width="192" TextOptions.TextRenderingMode="Aliased" FontFamily="Segoe UI" FontSize="100" x:FieldModifier="public" x:Name="middleForcastCurrentTemp"   
  7.                                             FontWeight="Medium" TextWrapping="Wrap" TextAlignment="Right" Stroke="Black" StrokeThickness="3" Fill="White" Text="10"/>  
  8.         </TextBlock>  
  9.     </Grid>  
 And in my MainWindow code:
  1. public MainWindow()  
  2.         {  
  3.             InitializeComponent();  
  4.   
  5.             curTemp _curTempWindow = new curTemp();  
  6.   
  7.             _curTempWindow._valTempChange = "55";  
  8.   
  9.         }  

When I run that code it never shows "55" in the textBlock. It only shows my default text "10".

What am I doing incorrect here?


Answers (2)