2009-11-15 6 views

답변

16
<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" /> 
+0

왜 모든 추가 코드가 필요합니까? 이 작업은 전적으로 XAML에서 처리됩니다. – Dave

+0

여분의 코드는'DataGridTextColumn','AutoCompleteBox' 및'ComboBox'에서 사용할 수있는 재사용 가능한 동작입니다. 여러분이 볼 수 있듯이 매번 setter를 통해 설정을 할애 할 수있는 첨부 된 MaxLength 속성입니다. – Shimmy

+0

바인딩을 사용하여이 값을 설정할 수 없습니다. 작동하지 않습니다. –

1

을 모든 열 중에서 공유 스타일을 사용하고 하나 이상의 스타일에 추가 스타일을 추가하려면 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>