【发布时间】:2014-09-11 10:12:24
【问题描述】:
我目前正在支持一个项目,并且我收到了一份更改数据网格单元验证的工作,以使他们插入的文本块的输入仅为 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;
}
}
图片
所以我需要知道如何获取我为此专栏输入的文本?
任何帮助都会很棒:)
谢谢。
【问题讨论】:
标签: c# wpf datagridtextcolumn