选择项目datagrid wpf时显示错误文本

时间:2019-02-16 13:16:18

标签: c# wpf xaml datagrid

我使用此代码

txt_f1.Text = datagrid_customer.SelectedCells[1].Column.GetCellContent(datagrid_customer.SelectedItem).ToString();

但是当我单击任何项​​目时

告诉我:

System.windows.controls.textblock

数据网格代码

<DataGrid HorizontalAlignment="Left" Height="100" Margin="861,-30,-261,0" 
          VerticalAlignment="Top" Width="100" Name="datagrid_customer"  
          SelectionChanged="datagrid_customer_SelectionChanged">
    <DataGrid.Columns>
        <DataGridTextColumn Header="کد محصول" Binding="{Binding MahID}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />
                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

1 个答案:

答案 0 :(得分:0)

如果您需要获取MahID,则应该能够从SelectedItem本身获取它。喜欢,

private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var grid = sender as DataGrid;
    var mahId = (grid.SelectedItem as YourModel).MahID;
}