Access the same data a textbox in the XAML is bound to

WPF MVVM in ViewModel I want to access the same data a textbox in the XAML is bound to

The XAML on MainWindow.xaml has a textbox bound to StoredProcs/ProcName

<TextBox Name="txtProcName" Text="{Binding Path=StoredProcs/ProcName}"></TextBox>

And a Grid bound to StoredProcs Whenever the grid selection changes, the bound text in the textbox changes as it should.

<DataGrid AutoGenerateColumns="False" 
    Height="300" Width="290"
    ItemsSource="{Binding StoredProcs}" 
    Margin="-6,0" Grid.RowSpan="2" Grid.Row="0">
        <DataGridTextColumn Header="Proc Name" Binding="{Binding ProcName}" >

I have a button that executes a procedure in MainWindowViewModel when clicked, that works

<Button Content="Create RDL" Command="{Binding CreateStoredProcedure}" />

In the CreateStoredProcedure code, I need to access the same data that is displayed in the textbox (not using code behind). I would think I need to get the StoredProcs/ProcName but can't figure out how to do that.

I tried adding CommandParameter to the XAML but don't know how to access it in the CreateStoredProcedure instructions as it won't allow me to add paramaters

void CreateStoredProcedureExecute()
    string procName = "proc";
    //procName = { StoredProcs/ProcName };
    MessageBoxResult result = 
        MessageBox.Show(String.Format("Create Stored Procedure {0}", procName));

bool CanCreateStoredProcedure()
    return true;

public ICommand CreateStoredProcedure 
        return new RelayCommand(CreateStoredProcedureExecute, 


Unless I am misunderstanding your question, you should be able to get the value of the property that the TextBox is bound to from within CreateStoredProcedure.

One thing though, if you want the TextBox to update the property, make sure you add "Mode=TwoWay" to your binding expression:

<TextBox Name="txtProcName" Text="{Binding Path=StoredProcs/ProcName, Mode=TwoWay}"></TextBox>

Unless I misunderstood I think you want something like this?

  Content="Create RDL" 
  Command="{Binding CreateStoredProcedure}" 
  CommandParameter="{Binding ElementName=txtProcName, Path=Text}"/>

However as the other answer states, you should be able to just access the property in the ViewModel that is backing the textbox from the command, but if for some reason you cannot my code should work as well.

(Assuming you are defining RelayCommand as defined by this MSDN article, this should fix your other problem)

public ICommand CreateStoredProcedure 
        return new RelayCommand<object>(
            (object parameter) => CreateStoredProcedureExecute(parameter), 
            (object parameter) => CanCreateStoredProcedure); 

private void CreateStoredProcedureExecute(object parameter)
    string ProcName = parameter as string;

I will admit my somewhat inexperience with setting up commands like this, but I did find a working example in my code that followed this, so hopefully it helps.

I think KDiTraglia has the right solution. The only thing I would do differently is to bind the CommandParameter to the model, not the UI element.

     Content="Create RDL"
     Command="{Binding CreateStoredProcedure}" 
     CommandParameter="{Binding Path=StoredProcs/ProcName}" />

I'm assuming that StoredProcs/ProcName is a placeholder for a real, valid binding path.

Root around here for more information:

Need Your Help

How to find memory leak in a Phonegap application

javascript iphone ios cordova

I have developed a phonegap application. Now I need to find memory leak in javascript code. I tried using instrument (supplied with xcode), but that doesn't help much in javascript. Could anyone pl...

How can I override the name attribute of a RadioButtonFor?

html razor html-helper radiobuttonfor

I'm trying to group together radiobuttons that are creating using a for loop and Razor syntax. Here is the code: