0

i want to send Data from one Textbox on window One to a label on window Two.

starting with window two:

<StackPanel>
    <StackPanel x:Name="ButtonStackPanel" Height="Auto" Width="Auto">
        <StackPanel Orientation="Horizontal">
            <StackPanel Orientation="Vertical">
                <Button Style="{DynamicResource ButtonStyle}" Content="To The Dark Side" Click="OnClickToDarkSide"/>
                <Button Style="{DynamicResource ButtonStyle}" Content="To The Gray" Click="OnClickToGraySide"/>
                <Button Style="{DynamicResource ButtonStyle}" Content="To The Light Side" Click="OnClickToLightSide"/>
            </StackPanel>
            <Border HorizontalAlignment="Center" VerticalAlignment="Stretch" Background="Red" Height="Auto" Width="2"/>
            <Label Style="{DynamicResource LabelStyle}" x:Name="theTextBlock" Content="{Binding Source=CodeText}"/>
            <Border HorizontalAlignment="Center" VerticalAlignment="Stretch" Background="Red" Height="Auto" Width="2"/>
            <ToggleButton Style="{DynamicResource ToggleButtonStyle}" Content="Open Style Window" Name="StyleWindowButton" Click="OnClickOpenStyleWindow"/>
            <ToggleButton Style="{DynamicResource ToggleButtonStyle}" Content="Open Text Window" Name="TextWindowButton" Click="OnClickOpenTextWindow"/>
        </StackPanel>
        <Border Height="2" Width="Auto" Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
    </StackPanel>
    <Border Height="2" Width="Auto" Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
</StackPanel>

Codebehind of Window Two:

       public MainWindow()
        {
            (App.Current as App).CodeText = _jediCode;
            InitializeComponent();
        }

        private void OnClickToDarkSide(object sender, RoutedEventArgs e)
        {
            (App.Current as App).ChangeSkin(Skin.Dark);
            (App.Current as App).CodeText = _sithCode;
            theTextBlock.Content = (App.Current as App).CodeText;
        }

        private void OnClickToLightSide(object sender, RoutedEventArgs e)
        {
            (App.Current as App).ChangeSkin(Skin.Light);
            (App.Current as App).CodeText = _jediCode;
            theTextBlock.Content = (App.Current as App).CodeText;
        }

        private void OnClickToGraySide(object sender, RoutedEventArgs e)
        {
            (App.Current as App).ChangeSkin(Skin.Gray);
            (App.Current as App).CodeText = _grayCode;
            theTextBlock.Content = (App.Current as App).CodeText;
        }

        private void OnClickOpenStyleWindow(object sender, RoutedEventArgs e)
        {
            if (StyleWindowButton.IsChecked == true)
            {
                styleWindow = new StyleWindow();
                styleWindow.Show();
            }
            else

            {
                styleWindow.Close();
                styleWindow = null;
            }


        }

        private void OnClickOpenTextWindow(object sender, RoutedEventArgs e)
        {
            if (TextWindowButton.IsChecked == true)
            {
                textWindow = new InputWindow();
                textWindow.Show();
            }
            else

            {
                textWindow.Close();
                textWindow = null;
            }
        }
    }

window one:

<Grid>
        <TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Height="200" Width="200" TextWrapping="Wrap" 
                 AcceptsReturn="True" AcceptsTab="True" Text="{Binding Path=CodeText, Source={x:Static Application.Current}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Margin="10,10,0,0">
            <!-- TODO: trigger change from this textbox to the textblock in mainwindow -->
        </TextBox>
    </Grid>

code behind of one is empty.

app.xaml.cs:

public string CodeText
{
  get => _codeText;
  set { _codeText = value; OnPropertyChanged(nameof(CodeText)); }
}

Ok, the current behavior is clicking on one of the buttons (Dark Side, Gray, Light Side) leads to changes in the CodeText Property, which leads to a change of the content of the label of Window Two and the text of TextBox of Window One. Changing the text of the TextBox, changes also the CodeText Property, but does not lead to a change in the label and thats confusing, why does it work the one way, but not the other. hope you have a hint for me. :) Maybe i missed a trigger or a kind of refresh for the label

0

1 Answer 1

1

bindings in window One and Two are set differently. (window Two does it wrong, Content="{Binding Source=CodeText}" is not valid binding)

in fact, window Two removes the binding by assigning CodeText directly as local value:

theTextBlock.Content = (App.Current as App).CodeText;

you should remove that line, and use the same binding as in window One:

<Label Style="{DynamicResource LabelStyle}" x:Name="theTextBlock" 
       Content="{Binding Path=CodeText, Source={x:Static Application.Current}}"/>
Sign up to request clarification or add additional context in comments.

1 Comment

ok thanks alot and thats funny i tried a similar solution, before this attempt, but i doesn't work for me, maybe a little misstake in the firstplace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.