Связывание значений Texbox в WPF MVVM - PullRequest
0 голосов
/ 03 сентября 2018

У меня есть образец 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"); 
        }
    }
}

ActivityAction_coment следующим образом

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 Ответ

0 голосов
/ 06 сентября 2018

Привязать к 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, используя свойство Remarks source:

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");
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...