0

我目前正在支持一个项目,并且我收到了一份更改数据网格单元验证的工作,以使他们插入的文本块的输入仅为 50 个字符。

我现在已经获得了 datagrid gotfocus() 事件,并检查了每个 DataGridTextColumn 的标题,如果正确的标题获得焦点,我为该列创建一个 Key_Down 事件。

在 Key_Down 事件中,我想测试当前字符串的长度,如果超过 50,我希望文本框不再添加到字符串中。但我的问题是,我没有从 DataGridTextColumn 获取文本。这是我的代码:

XAML

<DataGrid Grid.Column="3" GotFocus="transitRouteParticularsGrid_GotFocus" AutoGenerateColumns="False" x:Name="transitRouteParticularsGrid" Grid.ColumnSpan="9" Grid.Row="30" Grid.RowSpan="6" CanUserResizeColumns="False" CanUserSortColumns="False" CanUserResizeRows="False" CanUserReorderColumns="False">

<DataGridTextColumn x:Name="cmbDestination" Header="Destination" EditingElementStyle="{StaticResource errorStyle}" Width="200" Binding="{Binding CustomDestination, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}">                        
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="TextAlignment" Value="Center" />

                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>

   </DataGrid>

到目前为止我背后的代码

private void transitRouteParticularsGrid_GotFocus(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource.GetType() == typeof(DataGridCell))
            {
                DataGridCell ChosenItem = (DataGridCell)e.OriginalSource;

                if ((ChosenItem.Column.Header.ToString() == "Destination") && (Routevalue == "CUSTOM"))
                {
                    ChosenItem.KeyDown += ChosenItem_KeyDown;
                }
            }
        }


 void ChosenItem_KeyDown(object sender, KeyEventArgs e)
        {
            string curentText = "";
            int maxlen = 50;
            DataGridCell griddestination = (DataGridCell)sender;

            if (griddestination.Column.GetType() == typeof(DataGridTextColumn))
            {
                //i have no idea what should be here???
                //any other solution would also be appreciated

                var DestinationColumn = griddestination.Column;

                //Check for length

                if (curentText.Length > 50)
                {
                   //Do whatever to text
                }
                else
                {
                   // destination.Text = curentText;
                }
            }
            else
            {
                e.Handled = true;
            }         
        }

图片 运行时数据网格的外观

所以我需要知道如何获取我为本专栏输入的文字?

任何帮助都是极好的 :)

谢谢。

4

1 回答 1

1

您可以使用DataGridCell.Content属性获取单元格的可视化树,导航同样取决于定义的 errorStyle 模板EditingElementStyle="{StaticResource errorStyle}"

假设它基于TextBox这里是如何获取文本框

TextBox cellTextbox = (TextBox)griddestination.Content;

现在您可以使用它来获取文本值或操作。

以上仅在单元格处于编辑模式时才有效,否则单元格内容是 ElementStyle 定义的 TextBlock

为了安全起见,您可以写与

TextBox cellTextbox = griddestination.Content as TextBox;
if(cellTextbox != null)
{
   //your logic
}
于 2014-09-11T13:28:45.543 回答