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
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).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.