【问题标题】:Get text from DataGridTextColumn with the Textblox style in code behind在后面的代码中使用 Textblox 样式从 DataGridTextColumn 获取文本
【发布时间】: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


    【解决方案1】:

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

    假设它是基于 TextBox 的,这里是获取文本框的方法

    TextBox cellTextbox = (TextBox)griddestination.Content;
    

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

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

    为了安全起见,你可以写和

    TextBox cellTextbox = griddestination.Content as TextBox;
    if(cellTextbox != null)
    {
       //your logic
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多