Home > Software design >  Binding a text box to a button on a wpf form
Binding a text box to a button on a wpf form

Time:11-05

I have 3 buttons and 3 text fields. I want each button to execute the same method, but depending on what button is clicked, a text different text field should populate. I thought this could be accomplished by binding each text field to a different buttons data context but the field isn't populating. Here is my xaml -

<Window x:Class="vbaGitTool.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:vbaGitTool"
        mc:Ignorable="d"
        Title="Vba Git Tool" 
        Height="369" 
        Width="800" 
        MaxWidth="800" 
        MaxHeight="369"
        Background="Aquamarine">
    <Grid Margin="0,0,0,-1">
        <GroupBox Header="Create .txt File From Workbook" HorizontalAlignment="Left" Height="118" Margin="10,10,0,0" VerticalAlignment="Top" Width="772" BorderBrush="Black">
            <Grid Margin="0,0,0,4">
                <Button
                    x:Name="workbookSelect_btn"
                    Content="Select WB Path"
                    HorizontalAlignment="Left"
                    Margin="10,30,0,0"
                    VerticalAlignment="Top"
                    Width="102"
                    Click="Click_SelectFilePath" Height="23" />
                <TextBox 
                    x:Name="workbookPath_txt"
                    DataContext="{Binding 
                    ElementName=workbookSelect_btn,
                    Path=DataContext, 
                    Mode=OneWayToSource}"
                    HorizontalAlignment="Left" 
                    Height="23" 
                    Margin="126,30,0,0" 
                    TextWrapping="Wrap" 
                    VerticalAlignment="Top" 
                    Width="624"/>
            </Grid>
        </GroupBox>
        
        <GroupBox Header="Compare .txt Files" HorizontalAlignment="Left" Height="140" Margin="10,156,0,0" VerticalAlignment="Top" Width="772" BorderBrush="Black">
            <Grid Margin="0,0,0,-42">
                <Button 
                    x:Name="selectDev_btn"
                    Content="Select Dev File" 
                    HorizontalAlignment="Left" 
                    Margin="10,10,0,0" 
                    VerticalAlignment="Top" 
                    Width="96" 
                    Height="23"/>
                <Button 
                    x:Name="selectProd_btn"
                    Content="Select Prod File" 
                    HorizontalAlignment="Left" 
                    Margin="10,48,0,0" 
                    VerticalAlignment="Top" 
                    Width="96" 
                    Height="23"/>
                <TextBox 
                    x:Name="devFilePath" 
                    DataContext="{Binding 
                    ElementName=selectDev_btn,
                    Path=DataContext, 
                    Mode=OneWay}"
                    HorizontalAlignment="Left" 
                    Height="23" Margin="120,10,0,0" 
                    TextWrapping="Wrap" 
                    Text="" 
                    VerticalAlignment="Top" 
                    Width="630"/>
                <TextBox 
                    x:Name="prodFilePath" 
                    DataContext="{Binding 
                    ElementName=selectProd_btn,
                    Path=DataContext, 
                    Mode=OneWay}"
                    HorizontalAlignment="Left" 
                    Height="23" 
                    Margin="120,48,0,0" 
                    TextWrapping="Wrap" 
                    Text="" 
                    VerticalAlignment="Top" 
                    Width="630"/>
            </Grid>       
        </GroupBox>

    </Grid>
</Window>

And here's the reusable method

    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Click_SelectFilePath(object sender, RoutedEventArgs e)
        {

            System.Windows.Forms.OpenFileDialog ofd = 
                new System.Windows.Forms.OpenFileDialog();

            ofd.Title = "Find File";
            ofd.Filter = "Excel Files (*.xls) | *.xlsm | All Files(*.*) | *.*";  
            DialogResult dr = ofd.ShowDialog();

            this.DataContext = ofd.FileName;
        }
}

CodePudding user response:

In order to set the text in a TextBox - or perhaps better a TextBlock - you would set a Binding on its Text property, with data flow from source to target, not OneWayToSource:

<TextBlock Text="{Binding ElementName=workbookSelect_btn, Path=DataContext}"/>

or

<TextBlock Text="{Binding DataContext, ElementName=workbookSelect_btn}"/>

Now you would also have to set the Button's DataContext, not that of the Window:

((Button)sender).DataContext = ofd.FileName;

It does also look odd to use the DataContext for this purpose. Consider creating a view model to which all the UI elements in your Window bind their properties. Search the web for MVVM.

As a workaround, use the Tag property instead:

<TextBlock Text="{Binding Tag, ElementName=workbookSelect_btn}"/>

with

((Button)sender).Tag = ofd.FileName;
  • Related