0

can you please help me.

Im new in WPF and trying absolutely simple code. Have form with open save dialog. Path to a file im fill to a textblock and this string i need use in other module.

Without WPF i know how i can use it

frm_main.txt_path.text

But i cannot find this in WPF. This is my form. And i need use txt_path textblock

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:SCM_TO_BIESSE"
        mc:Ignorable="d"
        Title="Conversion SCM XXL code to Biesse XNC" Height="413.059" Width="647.202">
    <Grid x:Name="frm_main" Margin="1,1,1,1.2">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0*"/>
            <ColumnDefinition Width="194*"/>
            <ColumnDefinition Width="445*"/>
        </Grid.ColumnDefinitions>
        <Grid.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="Black" Offset="0"/>
                <GradientStop Color="#FF4396E4" Offset="1"/>
            </LinearGradientBrush>
        </Grid.Background>
        <TextBlock x:Name="txt_path" HorizontalAlignment="Left" Margin="68,196,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="48" Width="285" Grid.ColumnSpan="3" Foreground="#FF6B5050">
            <TextBlock.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF897F7F" Offset="0"/>
                    <GradientStop Color="#FFFAFAFA" Offset="1"/>
                </LinearGradientBrush>
            </TextBlock.Background>
        </TextBlock>
        <Button x:Name="btn_open" Content="Select file for conversion" HorizontalAlignment="Left" Margin="211.4,196,0,0" VerticalAlignment="Top" Width="160" Height="48" Grid.Column="2" RenderTransformOrigin="0.377,-0.663"/>
        <Image Grid.Column="1" Margin="1,1,317.4,295" Source="new.jpg" Stretch="Fill" Grid.ColumnSpan="2"/>
        <Button x:Name="btn_open_Copy" Content="Start Conversion" HorizontalAlignment="Left" Margin="65.4,304,0,29" Width="160" Grid.Column="2" RenderTransformOrigin="0.377,-0.663"/>

    </Grid>
</Window>

Thanks very much for a help .

Marek

5
  • Firstly, fix the title of your question. This thread has nothing at all to do with VBA and you're not supposed to put tags in the title anyway, so WPF goes too. The title is supposed to summarise what the question is about. If you can't come up with a suitable title then you haven't given the question enough thought to be posting it at all. Commented Aug 6, 2018 at 0:19
  • Sorry, my fault. Thanks Commented Aug 6, 2018 at 0:22
  • It appears to be a textblock not a textbox. Commented Aug 6, 2018 at 0:50
  • Yes, i see this also now, but problem is same. Commented Aug 6, 2018 at 0:54
  • If you're calling yourTextBox inside the window class markup, then something like txt_path.Text must work. If you're calling it outside.... no don't call it outside. You'd better use some Binding. I know it may sounds difficult at first but you should try reading this tutorial TextBox and this one DataBinding. Commented Aug 6, 2018 at 1:11

1 Answer 1

1

WPF is predicated (based, built) on binding to achieve a separation of concerns. You can do things the same way as WinForms but it isn't advised.

First (as mentioned in a comment) you need to use a TextBox, not a TextBlock. You haven't bound your TextBox to an underlying property (note that I've simplified this):

    <TextBox x:Name="txt_path" 
             Text="{Binding YourTextProperty}"  
             Height="48" Width="285" 
             >

    </TextBox>

</Grid>

You then have a public get/set string property called YourTextProperty on your viewmodel - then anything populated in to that TextBox will get automatically propagated to the underlying property (and vice versa - if you've correctly implemented INPC then any value populated into the property will get reflected in the textbox).

Sign up to request clarification or add additional context in comments.

Comments

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.