WPF MVVM中的Texbox值绑定

时间:2018-09-03 08:27:58

标签: c# wpf xaml mvvm

我有带有class Room { public int ID; public string Name; public int Completed = 0; public Random r = new Random(); public static Random r1 = new Random(); public Room(int id, string name) { this.ID = id; this.Name = name; } public void Passage(Room room, Hero hero, ConsoleKeyInfo direction) { bool checkroom = room.CheckCompletetion(room); if (checkroom == true) { Console.WriteLine("empty"); } if (checkroom == false) { room.GenerateInteraction(room, hero); } } public bool CheckCompletetion(Room room) { if (room.Completed == 1) { return true; } else { return false; } } 代码的示例XAML:

XAML

TextBox

查看模型:

<TextBox HorizontalAlignment="Stretch"  VerticalAlignment="Center" 
         Text="{Binding Path=Remarks, UpdateSourceTrigger=PropertyChanged}"
         BorderThickness="0.5" Margin="0" Height="50" Background="Transparent" Foreground="White"  />

<Button CommandParameter="{Binding ListExecActionId}" 
        Command="{Binding Source={StaticResource Locator}, Path=TaskPerformanceModel.ActivityAction_comment}"
        Content="Save"  HorizontalAlignment="Right" VerticalAlignment="Center" Margin="3,0,0,0" Height="Auto" />

ActivityAction_coment如下

public string Remarks
{
    get { return _remarks; }
    set
    {               
        if (!string.Equals(_remarks, value))
        {
            _remarks = value;
            RaisePropertyChanged("Remarks"); 
        }
    }
}

public RelayCommand<object> ActivityAction_comment
{
    get
    {
        if (_ActivityAction_comment == null)
        {
            _ActivityAction_comment = new RelayCommand<object>((ExecActionId) => ActivityComment(ExecActionId));
        }
        return _ActivityAction_comment;
    }
}

我无法在视图模型中获取文本框(备注)值。总是得到private void ActivityComment(object _id) { try { using (DataContext objDataContext = new DataContext(DBConnection.ConnectionString)) { ListExecutionAction tblListExec = objDataContext.ListExecutionActions.Single(p => p.Id == Convert.ToInt32(_id)); **tblListExec.Remarks = Remarks; // Not getting Remarks value from Textbox** objDataContext.SubmitChanges(); } } catch (Exception Ex) { MessageBox.Show(Ex.Message, "TaskExecution:ActivityComment"); } } 。 有人可以帮我吗?

为了更加清晰,我正在更新视图:

""

1 个答案:

答案 0 :(得分:1)

绑定到视图模型的ActivityAction_commentRemarks属性:

<Button CommandParameter="{Binding ListExecActionId}"
        Command="{Binding DataContext.ActivityAction_comment, RelativeSource={RelativeSource AncestorType=ListView}}" 
        Content="Save" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="3,0,0,0"  Height="Auto" />

您需要对“备注”绑定进行相同的操作

<TextBox Text="{Binding DataContext.Remarks, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType=ListView}}" ... />

然后,您应该可以使用TextBox源属性在Remarks中获取值:

private void ActivityComment(object _id)
{
    try
    {
        using (DataContext objDataContext = new DataContext(DBConnection.ConnectionString))
        {
            ListExecutionAction tblListExec = objDataContext.ListExecutionActions.Single(p => p.Id == Convert.ToInt32(_id));
            string remarks = Remarks;
            objDataContext.SubmitChanges();
        }
    }
    catch (Exception Ex)
    {
        MessageBox.Show(Ex.Message, "TaskExecution:ActivityComment");
    }
}