DataGridTemplate列,其中textbox不保留datagrid列字段中的值 [英] DataGridTemplate column with textbox not retaining the value in the datagrid column field

查看:316
本文介绍了DataGridTemplate列,其中textbox不保留datagrid列字段中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个只包含10个字符作为最大长度的文本。因为我发现,正常的Datagrid textcolumn多年平均值有MaxLength属性我去模板列。但是对于下面的xaml代码。数据不会保存到绑定字段,并显示旧值。 。任何人都可以伸出援助之手。



 <网格背景=#FFF0F0F0> 
将;数据网格AlternatingRowBackground =#FFE9FFE9AlternationCount =1的AutoGenerateColumns =假PreviewKeyDown =DgvDiagramNo_PreviewKeyDownCanUserAddRows =假CanUserResizeColumns =假ColumnHeaderHeight =30的DataContext ={结合} HEIGHT =482的ItemsSource ={绑定路径=,模式=双向}保证金=23,59,0,0NAME =DgvDiagramNoOverridesDefaultStyle =FALSERowHeaderWidth =0的rowHeight =30 ScrollViewer.VerticalScrollBarVisibility =自动的SelectionMode =单SelectionUnit =FullRowUseLayoutRounding =真VerticalAlignment =顶部的Horizo​​ntalAlignment =左WIDTH =958DataGridCell.Selected =DataGrid_selectCanUserReorderColumns =FALSECellStyle ={StaticResource的Body_Content_DataGrid_Centering}的KeyUp =DgvDiagramNo_KeyUpBeginningEdit =DgvDiagramNo_BeginningEditCellEditEnding =DgvDiagramNo_CellEditEnding排序=DgvDiagramNo_Sorting>
< DataGrid.Columns>
< DataGridTemplateColumn标题=売価WIDTH =150了minWidth =100了maxWidth =100>
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< TextBlock的文本={绑定路径= SellingPrice,模式=双向}/>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< DataGridTemplateColumn.CellEditingTemplate>
< DataTemplate>
< TextBox MaxLength =10/>
< / DataTemplate>
< /DataGridTemplateColumn.CellEditingTemplate>
< / DataGridTemplateColumn>


您忘记了 TextBox 中的 {Binding} > CellEditingTemplate

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

编辑:使用以下视图模型成功测试

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

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

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


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>

解决方案

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; }
}

这篇关于DataGridTemplate列,其中textbox不保留datagrid列字段中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆