0

私は現在プロジェクトをサポートしており、データグリッドセルの検証を変更して、挿入したテキストブロックの入力を 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;
            }         
        }

画像 実行時のデータグリッドの外観

この列に入力したテキストを取得する方法を知る必要がありますか?

どんな助けでも素晴らしいでしょう:)

ありがとう。

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 に答える