私は現在プロジェクトをサポートしており、データグリッドセルの検証を変更して、挿入したテキストブロックの入力を 50 文字だけにする仕事を受け取りました。
私は今行き、datagrid gotfocus() イベントを取得し、各 DataGridTextColumn のヘッダーをチェックし、正しいヘッダーが focus を取得した場合、列の 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;
}
}
画像
この列に入力したテキストを取得する方法を知る必要がありますか?
どんな助けでも素晴らしいでしょう:)
ありがとう。