如何在双击时将可观察的集合数据网格与文本框绑定?

我正在使用 MVVM 学习 WPF。


我有一个包含一些信息的 DataGrid。数据网格的来源是一个可观察的集合。当我双击数据网格中的特定行时,我需要将信息加载到文本框中。我希望将行的特定单元格加载到文本框中。


我遵循的步骤


声明了一个数据网格

窗口.xaml:


     <DataGrid x:Name="datagrid1" SelectedItem="{Binding GetRowData}" AutoGenerateColumns="True" CanUserAddRows="False"

Height="90" Width="358" Margin="-210,-200,-90,-187" HorizontalAlignment="Left">

                        <DataGrid.Style>

                        <Style TargetType="{x:Type DataGrid}">

                        <Setter Property="ItemsSource" Value="{Binding modelclasswithcombobox}"/>

                        <Style.Triggers>

                        <DataTrigger Binding="{Binding isfront}" Value="True">

                        <Setter Property="ItemsSource" Value="{Binding ModelClasstabwithdate}"/>

                       </DataTrigger>

                     </Style.Triggers>

                   </Style>

           </DataGrid.Style>

    </DataGrid>

声明一个文本框

在文本框中,我将选定的数据网格项目绑定为路径。


 <TextBox x:Name="txtboxAuditId" IsEnabled="{Binding IsEnabled}" 

  Width="108" Height="19.277" HorizontalAlignment="Left" 

 Margin="-20,14,708.962,0" VerticalAlignment="Top" Text="{Binding 

 ElementName=datagrid1, Path =  GetRowData, Mode=TwoWay, 

 ValidatesOnDataErrors=True,NotifyOnValidationError=True, 

 UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True}" />

在 viewmodel 中用 INotifyPropertyChanged 声明了一个属性来获取改变的数据。


private string _gettherow;

public string GetRowData

{

    get { return this._gettherow; }

    set

    {

        if (this._gettherow != value)

        {

            this._gettherow = value;

            OnPropertyChanged("GetRowData");

        }

     }

}

现在的问题是如何在我单击数据网格行时提供命令,以便填充我的文本框。我通常将 Icommand 界面用于按钮单击事件,无论在这里我是否需要应用相同的或其他任何东西。


慕丝7291255
浏览 55回答 2
2回答

呼啦一阵风

在这种情况下,您的 GetRowData 不应该是字符串。它应该是您的自定义类型,例如您的 ObservableCollection。例如,如果下面是绑定到 DataGrid 的 ObservableCollection,ObservableCollection<ModelClass> collection = new ObservableCollection<ModelClass>();那么你的 GetRowData 应该是这样的。private ModelClass _gettherow;public ModelClass GetRowData{&nbsp; &nbsp; get { return this._gettherow; }&nbsp; &nbsp; set&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (this._gettherow != value)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this._gettherow = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OnPropertyChanged("GetRowData");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;}}然后像这样将它绑定到 TextBox (CustomType.PropertyName)<TextBox Text="{Binding GetRowData.Name}"/>

精慕HU

因此,如果我理解了这个问题,您想TextBox在用户输入您的DataGrid Cellor时更新一个独立的Row。如果是这种情况,那么你想使用DataTriggers大意是什么&nbsp; &nbsp; &nbsp; &nbsp; <TextBox>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextBox.Style>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Style TargetType="TextBox">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Style.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataTrigger Binding="{Binding ElementName=yourDataGrid, Path=BeginningEdit}" Value="True">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="Text" Value="{Binding YourTextProperty}" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataTrigger>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Style.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Style>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </TextBox.Style>&nbsp; &nbsp; &nbsp; &nbsp; </TextBox>你可以在这里阅读更多关于它的信息https://www.wpf-tutorial.com/styles/trigger-datatrigger-event-trigger/
打开App,查看更多内容
随时随地看视频慕课网APP