DataGridTemplate column with textbox not retaining

2019-09-08 09:13发布

问题:

I wanted a text which holds only 10 characters as max length in it. As I found that normal Datagrid textcolumn doesnot have maxlength property I went for Template column. But for the below xaml code. The data is not saving back to the binded field and its showing the old value. Can anyone give a helping hand.

    <Grid Background="#FFF0F0F0">
        <DataGrid AlternatingRowBackground="#FFE9FFE9" AlternationCount="1" AutoGenerateColumns="False" PreviewKeyDown="DgvDiagramNo_PreviewKeyDown" CanUserAddRows="False" CanUserResizeColumns="False" ColumnHeaderHeight="30" DataContext="{Binding}" Height="482" ItemsSource="{Binding Path=., Mode=TwoWay}" Margin="23,59,0,0" Name="DgvDiagramNo" OverridesDefaultStyle="False" RowHeaderWidth="0" RowHeight="30" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionMode="Single" SelectionUnit="FullRow" UseLayoutRounding="True" VerticalAlignment="Top" HorizontalAlignment="Left" Width="958" DataGridCell.Selected="DataGrid_select" CanUserReorderColumns="False" CellStyle="{StaticResource Body_Content_DataGrid_Centering}"  KeyUp="DgvDiagramNo_KeyUp" BeginningEdit="DgvDiagramNo_BeginningEdit" CellEditEnding="DgvDiagramNo_CellEditEnding" Sorting="DgvDiagramNo_Sorting">
            <DataGrid.Columns>
<DataGridTemplateColumn Header="売価" Width="150" MinWidth="100" MaxWidth="100" >
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path= SellingPrice, Mode=TwoWay}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                        <DataGridTemplateColumn.CellEditingTemplate >
                            <DataTemplate>
                                <TextBox MaxLength="10"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellEditingTemplate>
                    </DataGridTemplateColumn>

回答1:

You forgot the {Binding} in the TextBox of the CellEditingTemplate

<DataGridTemplateColumn Header="売価" Width="150" MinWidth="100" MaxWidth="100" >
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding SellingPrice}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate >
        <DataTemplate>
            <TextBox MaxLength="10" Text="{Binding SellingPrice}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

EDIT: Successfully tested with the following View Model

public class RowViewModel
{
    public string SellingPrice { get; set; }
}

public class ViewModel
{
    public ViewModel()
    {
        Rows = new ObservableCollection<RowViewModel>
        {
              new RowViewModel { SellingPrice = "123" },
              new RowViewModel { SellingPrice = "456" }, 
        };
    }

    public IEnumerable<RowViewModel> Rows { get; set; }
}