CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2008
    Posts
    111

    Question WPF change UserControl textblock from another window

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

    Code:
    <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:
    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?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: WPF change UserControl textblock from another window

    You need to implement INotifyPropertyChanged. See http://www.wpftutorial.net/INotifyPropertyChanged.html

    Not that it matters, but you may want to follow the C# naming conventions for classes, properties, variables, etc. While it doesn't strictly matter for code that is only maintained by you, it does make it harder for others to maintain your code if you ignore the basic conventions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured