Can anyone give me an example of using a textbox to get user input?

2355
2
Jump to solution
08-21-2019 04:47 PM
SamRideout1
New Contributor III

Textboxes shouldn't be this hard to access.  I want to get a few pertinent info pieces from my user for a process and am trying to access some textboxes that I have created on either a pane or a window.  Am not able to find a simple way to get the text out of the textbox.

1 Solution

Accepted Solutions
SamRideout1
New Contributor III

I figured out a way that works for me.  In the Dockpane.xaml, add an x:Name=     property to the TextBox like this:

<TextBox x:Name="txtWatPolyElev" HorizontalAlignment="Left" Height="23"................

Then in the Dockpane.xaml.cs in the Button_Click method, access the text using the name like this:

string strElev = txtWatPolyElev.Text;

Simple and it works for me.

View solution in original post

2 Replies
by Anonymous User
Not applicable

Hi Sam,

I believe you can add the dock pane at the moment.

You will notice there are two files added into your project *.xaml and *viewmodel.cs

Double click and open the xaml file, you shall replace grid tag with below.

 <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <DockPanel Grid.Row="0" LastChildFill="true" KeyboardNavigation.TabNavigation="Local" Height="30">
            <TextBlock Grid.Column="1" Text="{Binding Heading}" Style="{DynamicResource Esri_TextBlockDockPaneHeader}">
                <TextBlock.ToolTip>
                    <WrapPanel Orientation="Vertical" MaxWidth="300">
                        <TextBlock Text="{Binding Heading}" TextWrapping="Wrap"/>
                    </WrapPanel>
                </TextBlock.ToolTip>
      </TextBlock>
        </DockPanel>
        <StackPanel Grid.Row="1" Margin="5">
            <TextBlock HorizontalAlignment="Left" Text="Your text" VerticalAlignment="Center"/>
            <TextBox Name="txtDebugMessage" MinWidth="250" Text="{Binding Path=DebugMessage}" ></TextBox>
        </StackPanel>
        <StackPanel Grid.Row="2">
            <Button Name="btnDisplayMessage" Command="{Binding Path=CmdDisplayMessage}" HorizontalAlignment="Left" Width="auto" >Display Message</Button>
        </StackPanel>
    </Grid>

In your *viewmodel.cs, append the below code.

private string _debugMessage;
        public string DebugMessage
        {
            get
            {
                return _debugMessage;
            }
            set
            {
                if (this._debugMessage != value)
                {
                    this._debugMessage = value;
                    this.NotifyPropertyChanged();
                }
            }
        }

        private ICommand _cmdDisplayMessage;
        public ICommand CmdDisplayMessage
        {
            get
            {
                return _cmdDisplayMessage ?? (_cmdDisplayMessage = new RelayCommand(() =>
                {
                    //Should not use QueueTask with OpenItemDialog
                    MessageBox.Show($"{DebugMessage}");
                }));
            }
        }
And import namespace => using System.Windows.Input;
And debug your code, you shall be able to see a textbox and button.
Upon button click, you can see alert with what you typed.
Explanation: {Binding Path=DebugMessage} is to bind the textbox dat to DebugMessage variable, Command="{Binding Path=CmdDisplayMessage}" is to bind the command.
ArcGIS pro sdk approach is MVVM and different from ArcMap.
Below is the reference which can help you with the concept.
0 Kudos
SamRideout1
New Contributor III

I figured out a way that works for me.  In the Dockpane.xaml, add an x:Name=     property to the TextBox like this:

<TextBox x:Name="txtWatPolyElev" HorizontalAlignment="Left" Height="23"................

Then in the Dockpane.xaml.cs in the Button_Click method, access the text using the name like this:

string strElev = txtWatPolyElev.Text;

Simple and it works for me.