代码之家  ›  专栏  ›  技术社区  ›  Hussain

wpfmvvm中的Texbox值绑定

  •  0
  • Hussain  · 技术社区  · 6 年前

    我有样品XAML TextBox

    XAML公司

    <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" />
    

    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");
        }
    }
    

    我无法在视图模型中获取文本框(备注)值。总是得到 "" .

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

    <ListView.View>                    
                      <GridViewColumn >
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                    <TextBlock Text="{Binding ActionDescription}" Foreground="White" FontSize="16"></TextBlock>
                                        <ToggleButton Name="button">
                                            <ToggleButton.Template>
                                                <ControlTemplate TargetType="ToggleButton">
                                                    <TextBlock>Remarks!!</TextBlock>
                                                </ControlTemplate>
                                            </ToggleButton.Template>
                                        </ToggleButton>
                                        <Popup IsOpen="{Binding IsChecked, ElementName=button}" StaysOpen="False" Width="250" Height="100">
                                            <StackPanel>
                                                <TextBlock Background="LightBlue" Text="{Binding ActionDescription}"></TextBlock>
    
                                                    <TextBlock Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center"  Text="Comments:" Foreground="White" Background="Transparent" />
                                                <TextBox HorizontalAlignment="Stretch"  VerticalAlignment="Center"                                                                                                                                
                                                         Text="{Binding Path=Remarks, UpdateSourceTrigger=PropertyChanged}" 
                                                     BorderThickness="0.5" Margin="0" Height="50"/>
    
                                                <!--Text="{Binding Remarks, Mode=OneWayToSource}" Text="{Binding Path=Remarks, UpdateSourceTrigger=PropertyChanged}"    DataContext="{Binding CollectionOfListQueue}" 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" />
                                                    <Button Content="Cancel" HorizontalAlignment="Right"  VerticalAlignment="Center" Margin="2,0,0,0" Height="Auto" />
                                                <!--</Grid>-->
                                            </StackPanel>
                                        </Popup>
                                    </StackPanel>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   mm8    6 年前

    绑定到 ActivityAction_comment Remarks 视图模型的属性:

    <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 使用 评论 源属性:

    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");
        }
    }