locked
WPF change UserControl textblock from another window RRS feed

  • Question

  • 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:

    public partial class curTemp : UserControl
    {
            public string _valTempChange
            {
                get { return middleForcastCurrentTemp.Text; }
                set { middleForcastCurrentTemp.Text = value; }
            }

            public curTemp()
            {
                InitializeComponent();
            }
     }

    Xaml of the above UserControl:

    <Grid>
            <TextBlock TextWrapping="Wrap" Padding="5" Foreground="White" Panel.ZIndex="7"           HorizontalAlignment="Left"VerticalAlignment="Top" Margin="15,-110,-43,0" Width="198" Height="122">
                <TextBlock.Effect>
                    <DropShadowEffect BlurRadius="4" Direction="0" ShadowDepth="0" />
                </TextBlock.Effect>
                        <outlineText:OutlinedTextBlock Height="146" Width="192" TextOptions.TextRenderingMode="Aliased"       FontFamily="Segoe UI" FontSize="100" x:FieldModifier="public" x:Name="middleForcastCurrentTemp" 
                                                FontWeight="Medium" TextWrapping="Wrap" TextAlignment="Right" Stroke="Black" StrokeThickness="3" Fill="White" Text="10"/>
            </TextBlock>
    </Grid>

    And in my MainWindow code

    public MainWindow()
     {
                InitializeComponent();

                curTemp _curTempWindow = new curTemp();

                _curTempWindow._valTempChange = "55";

     }

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

    What am I doing incorrectly here?

    -Mrutyunjaya


    Wednesday, August 1, 2018 6:40 PM

Answers

  • Hi Mrutynjaya M,

    According to your description, you have three Window, one is usercontrol, another is curTemp window, another is Mainwindow, so you need to add Usercontrol control in curTemp window, and then changing text value in Mainwindow, and show curTemp window, you can see the value have been changed.

    I do one sample that you can take a look:

    Usercontrol:

    <Grid>
            <TextBlock
                Name="textblock1"
                Width="200"
                Height="30">
                <TextBlock
                    Name="textblock2"
                    Width="100"
                    Height="30"
                    x:FieldModifier="public"
                    Text="10" />
            </TextBlock>
        </Grid>

    Window 10:

     <StackPanel>
            <usercontrol:UserControl1 x:Name="usercontrol" Width="200" Height="30" Margin="0,10,10,10" ></usercontrol:UserControl1>
        </StackPanel>
     public partial class Window10 :Window
        {
           
            private string _textvalue;
            public string textvalue
            {
                get { return usercontrol.textblock2.Text; }
                set
                {
                    usercontrol.textblock2.Text = value;
    
                }
            }
            public Window10()
            {
                InitializeComponent();
            }
        }

    Mainwindow:

        <StackPanel>
    
            <Button Name="btn1" Width="100" Height="30" Content="clickme" Click="btn1_Click"></Button>
        </StackPanel>
     private void btn1_Click(object sender, RoutedEventArgs e)
            {
                Window10 window = new Window10();
                window.textvalue = "20";
                window.ShowDialog();
            }

    You can see that the textblock text value is 10 in usercontrol, and I change this value in Mainwindow.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 3, 2018 3:34 AM

All replies

  • Hi Mrutyunjaya,

    According to your description and code, you just want to change curTemp._valTempChange property, you have instantiated curTemp window, and changed _valTempChange property, if you want to see this effect, you can show this window using show() or shoudialog().

    curTemp _curTempWindow = new curTemp();
    
                _curTempWindow._valTempChange = "55";
                _curTempWindow.ShowDialog();

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 2, 2018 2:55 AM
  • I am showing this window using ShowDialog();

    _curTempWindow.ShowDialog();

    It shows my default text "10";

    -Mrutyunjaya

    Thursday, August 2, 2018 7:56 PM
  • Hi Mrutynjaya M,

    According to your description, you have three Window, one is usercontrol, another is curTemp window, another is Mainwindow, so you need to add Usercontrol control in curTemp window, and then changing text value in Mainwindow, and show curTemp window, you can see the value have been changed.

    I do one sample that you can take a look:

    Usercontrol:

    <Grid>
            <TextBlock
                Name="textblock1"
                Width="200"
                Height="30">
                <TextBlock
                    Name="textblock2"
                    Width="100"
                    Height="30"
                    x:FieldModifier="public"
                    Text="10" />
            </TextBlock>
        </Grid>

    Window 10:

     <StackPanel>
            <usercontrol:UserControl1 x:Name="usercontrol" Width="200" Height="30" Margin="0,10,10,10" ></usercontrol:UserControl1>
        </StackPanel>
     public partial class Window10 :Window
        {
           
            private string _textvalue;
            public string textvalue
            {
                get { return usercontrol.textblock2.Text; }
                set
                {
                    usercontrol.textblock2.Text = value;
    
                }
            }
            public Window10()
            {
                InitializeComponent();
            }
        }

    Mainwindow:

        <StackPanel>
    
            <Button Name="btn1" Width="100" Height="30" Content="clickme" Click="btn1_Click"></Button>
        </StackPanel>
     private void btn1_Click(object sender, RoutedEventArgs e)
            {
                Window10 window = new Window10();
                window.textvalue = "20";
                window.ShowDialog();
            }

    You can see that the textblock text value is 10 in usercontrol, and I change this value in Mainwindow.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 3, 2018 3:34 AM