wpf——datagrid中如何获取DataGridTextColumn等某行某列的值

时间:2022-08-11 16:20:58

如果使用的是mvvm模式的话,可以像下面这么做:

xaml:
<UserControl x:Class="CustomSetSN"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:Customer.MvvM"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:DesignHeight="340" d:DesignWidth="538">
    <UserControl.DataContext>
        <local:CViewModel/>
    </UserControl.DataContext>
    <Grid Background="#FF7EDE75">
        <Grid.RowDefinitions>
            <RowDefinition Height="81*"/>
            <RowDefinition Height="89*"/>
        </Grid.RowDefinitions>
        <DataGrid x:Name="dataGrid1" Background="#FF7EDE75" AutoGenerateColumns="False" CanUserAddRows="False"  HeadersVisibility="Column" HorizontalAlignment="Left" Margin="-7,-6,-3,0" VerticalAlignment="Top" Width="548" Height="346"  Loaded="ItemsSources_Load" Grid.RowSpan="2">
            <DataGrid.ColumnHeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGrid.ColumnHeaderStyle>
            <DataGrid.Columns >

                <DataGridTextColumn Header="设备总编号" x:Name="curtainEquTotalNum" Width="19*" Binding="{Binding EquTotalNum,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</UserControl>



后台函数中可以这样写:
var selectItem = dataGrid1.SelectedItem as SmartDbViewModel;//!根据点击的item获取集合中的数据

string currentChoicEquT = selectItem.EquTotalNum.ToString();

mvvm中EquTotalNum实现方式:
public string EquTotalNum
{
get { return _EquTotalNum; }
set
{
if (_EquTotalNum != value)
{
_EquTotalNum = value;
RaisePropertyChanged("EquTotalNum");
}
}
}

其他的一样的获取。