David Beck
David Beck

Reputation: 297

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"
    HorizontalAlignment="Center" 
    Name="dataGrid1" 
    VerticalAlignment="Top" 
    ItemsSource="{Binding StoredProcs}" 
    IsSynchronizedWithCurrentItem="True" 
    Margin="-6,0" Grid.RowSpan="2" Grid.Row="0">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Proc Name" Binding="{Binding ProcName}" >
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

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 
{ 
    get 
    { 
        return new RelayCommand(CreateStoredProcedureExecute, 
            CanCreateStoredProcedure); 
    } 
}

Upvotes: 0

Views: 386

Answers (3)

code4life
code4life

Reputation: 15794

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.

<Button 
     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: http://msdn.microsoft.com/en-us/library/ms752308

Upvotes: 0

Kevin DiTraglia
Kevin DiTraglia

Reputation: 26058

Unless I misunderstood I think you want something like this?

<Button 
  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 
{ 
    get 
    { 
        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.

Upvotes: 0

Steve Konves
Steve Konves

Reputation: 2668

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>

Upvotes: 1

Related Questions