如何从XAML中的文本框中获取值?

时间:2014-08-23 20:41:06

标签: c# wpf database xaml

我试图从文本框中获取价值,以便用。更新我的数据库。

这是我的XAML代码:

<Window x:Class="Banken.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:Banken.viewmodel"
        xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF45"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        Title="Bank" Height="350" Width="525">
    <Window.DataContext>
        <vm:AccountVM/>
    </Window.DataContext>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="250"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <ListBox Margin="8" ItemsSource="{Binding Accounts}" SelectedItem="{Binding SelectedAcc}" DisplayMemberPath="AccountHolder"/>
        <StackPanel Margin="8" Grid.Column="1">
            <Label Content="Accountholder:"/>
            <Label FontWeight="Bold" FontSize="10" Height="25px" Content="{Binding SelectedAcc.AccountHolder}"/>
            <Label Content="Accountnumber:"/>
            <Label FontWeight="Bold" FontSize="10" Height="25px" Content="{Binding SelectedAcc.AccountNumber}"/>
            <Label Content="Balance:"/>
            <Label FontWeight="Bold" FontSize="10" Height="25px" Content="{Binding SelectedAcc.Balance}"/>
            <Label Content="Transfer to:"/>
            <ComboBox ItemsSource="{Binding Path=Accounts}" DisplayMemberPath="AccountHolder" SelectedValuePath="AccountHolder" SelectedValue="{Binding Path=Accounts}" />
            <Label Content="Amount:"/>
            <TextBox InputScope="Number" Name="Amount"></TextBox>
            <StackPanel Orientation="Horizontal" Margin="0,16,0,0">
                <Button Content="Transfer money" Command="{Binding UpdateAccount}" Width="250"/>
            </StackPanel>
        </StackPanel>
    </Grid>

</Window>

现在我想从名为&#34; Amount&#34;的文本框中获取值,因此我可以像这样更新我的数据库:

 public ICommand UpdateAccount
 {
     get { return new RelayCommand<string>(UpdateAccount1); }
 }

 public void UpdateAccount1(string name)
 {
     Console.WriteLine(name);
     Account.UpdateAccount(SelectedAcc, *AMOUNT HERE*);
 }

然后我将它传递到我的数据库中:

public static void UpdateAccount(Account a, int Amount)
{
    string sql = "UPDATE Accounts SET Saldo=Saldo-@Amount WHERE ID=@ID";
    DbParameter par1 = Database.AddParameter("@Amount", Amount);
    DbParameter par2 = Database.AddParameter("@ID", a.ID);
    Database.ModifyData(sql, par1, par2);

    Console.WriteLine(a.ID + " was edited");
}

当我对其中的值进行硬编码时,这一切都非常有效,但我似乎无法找到在那里获取该文本框值的方法吗?

2 个答案:

答案 0 :(得分:1)

试试这个

    <Button Content="Transfer money" Command="{Binding UpdateAccount}" CommandParameter="{Binding Text, ElementName=Amount}" Width="250"/>

这里我们将CommandParameter绑定到TextBox文本。

答案 1 :(得分:0)

将按钮上的命令参数绑定到文本框的文本属性...

{Binding Text,elementname = ...}

...或将文本框的text属性绑定到viewmodel中的值。然后在你的命令行动中捡起它。