DataGridTextColumn.MaxLength?

发布于 2024-08-11 01:35:38 字数 46 浏览 6 评论 0原文

如何设置 DataGridTextColumn 的 MaxLength 属性?

How do I set the MaxLength property of the DataGridTextColumn?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

归属感 2024-08-18 01:35:38
<tk:DataGridTextColumn Binding="{Binding Text}">
    <tk:DataGridTextColumn.EditingElementStyle>
        <Style TargetType="TextBox">
            <Setter Property="MaxLength" Value="16"/>
        </Style>
    </tk:DataGridTextColumn.EditingElementStyle>
</tk:DataGridTextColumn>

您还可以使用以下行为来设置它,这样您就不必每次都使用样式和设置器:

Public Class TextBoxBehavior
    Private Shared Types As Type() = New Type() {GetType(AutoCompleteBox), GetType(ComboBox), GetType(DataGridTextColumn)}

    Public Shared Function GetMaxLength(ByVal element As DependencyObject) As Integer
        Return element.GetValue(MaxLengthProperty)
    End Function

    Public Shared Sub SetMaxLength(ByVal element As DependencyObject, ByVal value As Integer)
        element.SetValue(MaxLengthProperty, value)
    End Sub

    Private Shared Sub ValidateElement(ByVal element As DependencyObject)
        If element Is Nothing Then Throw New ArgumentNullException("element")
        If Not Types.Contains(element.GetType) Then Throw New NotSupportedException("The TextBoxBehavior is not supported for the given element")
    End Sub

    Public Shared ReadOnly MaxLengthProperty As DependencyProperty = 
     DependencyProperty.RegisterAttached("MaxLength", 
                        GetType(Integer), GetType(TextBoxBehavior), 
                        New FrameworkPropertyMetadata(Integer.MaxValue, AddressOf TextBox_MaxLengthChanged))

    Private Shared Sub TextBox_MaxLengthChanged(ByVal sender As Object, ByVal e As DependencyPropertyChangedEventArgs)
        If sender Is Nothing Then Exit Sub

        Dim value = DirectCast(e.NewValue, Integer)

        If TypeOf sender Is AutoCompleteBox Then
            Dim acb = DirectCast(sender, AutoCompleteBox)

            If acb.IsLoaded Then
                Dim tb = DirectCast(acb.Template.FindName("Text", acb), TextBox)
                tb.MaxLength = value
            Else
                acb.AddHandler(AutoCompleteBox.LoadedEvent, New RoutedEventHandler(AddressOf Element_Loaded))
            End If
        ElseIf TypeOf sender Is ComboBox Then
            Dim cb = DirectCast(sender, ComboBox)
            If cb.IsLoaded Then
                Dim tb = DirectCast(cb.Template.FindName("PART_EditableTextBox", cb), TextBox)
                tb.MaxLength = value
            Else
                cb.AddHandler(ComboBox.LoadedEvent, New RoutedEventHandler(AddressOf Element_Loaded))
            End If
        ElseIf TypeOf sender Is DataGridTextColumn Then
            Dim dgtc = DirectCast(sender, DataGridTextColumn)

            Dim setter = GetIsMaxLengthSet(dgtc.EditingElementStyle)
            If setter Is Nothing Then
                Dim style = New Style(GetType(TextBox), dgtc.EditingElementStyle)
                style.Setters.Add(New Setter(TextBox.MaxLengthProperty, value))
                dgtc.EditingElementStyle = style
                style.Seal()
            Else
                setter.Value = value
            End If
        End If
    End Sub

    Private Shared Function GetIsMaxLengthSet(ByVal style As Style) As Setter
        If style Is Nothing Then Return Nothing
        Dim setter = style.Setters.LastOrDefault(Function(s) TypeOf s Is Setter AndAlso DirectCast(s, Setter).Property Is TextBox.MaxLengthProperty)
        If setter IsNot Nothing Then Return setter Else Return GetIsMaxLengthSet(style.BasedOn)
    End Function

    Private Shared Sub Element_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim ml = GetMaxLength(sender)
        TextBox_MaxLengthChanged(sender, New DependencyPropertyChangedEventArgs(TextBox.MaxLengthProperty, -1, ml))
        sender.RemoveHandler(FrameworkElement.LoadedEvent, New RoutedEventHandler(AddressOf Element_Loaded))
    End Sub
End Class

用法:

<ComboBox xmlns:loc="MyNamesapace" loc:TextBoxBehavior.MaxLength="50" />
<tk:DataGridTextColumn Binding="{Binding Text}">
    <tk:DataGridTextColumn.EditingElementStyle>
        <Style TargetType="TextBox">
            <Setter Property="MaxLength" Value="16"/>
        </Style>
    </tk:DataGridTextColumn.EditingElementStyle>
</tk:DataGridTextColumn>

You could also set it using the following behavior so you don't have to use a style and setters each time:

Public Class TextBoxBehavior
    Private Shared Types As Type() = New Type() {GetType(AutoCompleteBox), GetType(ComboBox), GetType(DataGridTextColumn)}

    Public Shared Function GetMaxLength(ByVal element As DependencyObject) As Integer
        Return element.GetValue(MaxLengthProperty)
    End Function

    Public Shared Sub SetMaxLength(ByVal element As DependencyObject, ByVal value As Integer)
        element.SetValue(MaxLengthProperty, value)
    End Sub

    Private Shared Sub ValidateElement(ByVal element As DependencyObject)
        If element Is Nothing Then Throw New ArgumentNullException("element")
        If Not Types.Contains(element.GetType) Then Throw New NotSupportedException("The TextBoxBehavior is not supported for the given element")
    End Sub

    Public Shared ReadOnly MaxLengthProperty As DependencyProperty = 
     DependencyProperty.RegisterAttached("MaxLength", 
                        GetType(Integer), GetType(TextBoxBehavior), 
                        New FrameworkPropertyMetadata(Integer.MaxValue, AddressOf TextBox_MaxLengthChanged))

    Private Shared Sub TextBox_MaxLengthChanged(ByVal sender As Object, ByVal e As DependencyPropertyChangedEventArgs)
        If sender Is Nothing Then Exit Sub

        Dim value = DirectCast(e.NewValue, Integer)

        If TypeOf sender Is AutoCompleteBox Then
            Dim acb = DirectCast(sender, AutoCompleteBox)

            If acb.IsLoaded Then
                Dim tb = DirectCast(acb.Template.FindName("Text", acb), TextBox)
                tb.MaxLength = value
            Else
                acb.AddHandler(AutoCompleteBox.LoadedEvent, New RoutedEventHandler(AddressOf Element_Loaded))
            End If
        ElseIf TypeOf sender Is ComboBox Then
            Dim cb = DirectCast(sender, ComboBox)
            If cb.IsLoaded Then
                Dim tb = DirectCast(cb.Template.FindName("PART_EditableTextBox", cb), TextBox)
                tb.MaxLength = value
            Else
                cb.AddHandler(ComboBox.LoadedEvent, New RoutedEventHandler(AddressOf Element_Loaded))
            End If
        ElseIf TypeOf sender Is DataGridTextColumn Then
            Dim dgtc = DirectCast(sender, DataGridTextColumn)

            Dim setter = GetIsMaxLengthSet(dgtc.EditingElementStyle)
            If setter Is Nothing Then
                Dim style = New Style(GetType(TextBox), dgtc.EditingElementStyle)
                style.Setters.Add(New Setter(TextBox.MaxLengthProperty, value))
                dgtc.EditingElementStyle = style
                style.Seal()
            Else
                setter.Value = value
            End If
        End If
    End Sub

    Private Shared Function GetIsMaxLengthSet(ByVal style As Style) As Setter
        If style Is Nothing Then Return Nothing
        Dim setter = style.Setters.LastOrDefault(Function(s) TypeOf s Is Setter AndAlso DirectCast(s, Setter).Property Is TextBox.MaxLengthProperty)
        If setter IsNot Nothing Then Return setter Else Return GetIsMaxLengthSet(style.BasedOn)
    End Function

    Private Shared Sub Element_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim ml = GetMaxLength(sender)
        TextBox_MaxLengthChanged(sender, New DependencyPropertyChangedEventArgs(TextBox.MaxLengthProperty, -1, ml))
        sender.RemoveHandler(FrameworkElement.LoadedEvent, New RoutedEventHandler(AddressOf Element_Loaded))
    End Sub
End Class

Usage:

<ComboBox xmlns:loc="MyNamesapace" loc:TextBoxBehavior.MaxLength="50" />
躲猫猫 2024-08-18 01:35:38

如果所有列之间有共享样式,并且您想向其中一个或多个列添加附加样式,则可以使用 Style.BasedOn 属性:

 <DataGridTextColumn Binding="{Binding SomeProperty, UpdateSourceTrigger=PropertyChanged}" ElementStyle="{StaticResource CellErrorStyle}">
     <DataGridTextColumn.EditingElementStyle>
           <Style TargetType="TextBox" BasedOn="{StaticResource OriginalStyleKey}">
                <Setter Property="MaxLength" Value="5" />                            
           </Style>
     </DataGridTextColumn.EditingElementStyle>
 </DataGridTextColumn>

If you have a shared style among all columns, and you would like to add an additional style to one or more of those, you could use the Style.BasedOn Property:

 <DataGridTextColumn Binding="{Binding SomeProperty, UpdateSourceTrigger=PropertyChanged}" ElementStyle="{StaticResource CellErrorStyle}">
     <DataGridTextColumn.EditingElementStyle>
           <Style TargetType="TextBox" BasedOn="{StaticResource OriginalStyleKey}">
                <Setter Property="MaxLength" Value="5" />                            
           </Style>
     </DataGridTextColumn.EditingElementStyle>
 </DataGridTextColumn>
因为看清所以看轻 2024-08-18 01:35:38
     <Window.Resources>
        <Style x:Key="sty_txtDesc" TargetType="TextBox">
            <Setter Property="MaxLength" Value="495" />
        </Style>
    </Window.Resources>


    for (int i = 0; i < miDataGridX.Columns.Count; i++)
    {
        if (miDataGridX.Columns[i].Header.ToString() == "Description")
        {
            ((DataGridTextColumn)miDataGridX.Columns[i]).EditingElementStyle = (Style)this.FindResource("sty_txtDesc");
        }
    }
     <Window.Resources>
        <Style x:Key="sty_txtDesc" TargetType="TextBox">
            <Setter Property="MaxLength" Value="495" />
        </Style>
    </Window.Resources>


    for (int i = 0; i < miDataGridX.Columns.Count; i++)
    {
        if (miDataGridX.Columns[i].Header.ToString() == "Description")
        {
            ((DataGridTextColumn)miDataGridX.Columns[i]).EditingElementStyle = (Style)this.FindResource("sty_txtDesc");
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文