내 코드 전체에서 사용할 수있는 텍스트 상자의 스타일을 만들려고합니다. 내 스타일은 Text 속성의 바인딩에 변환기를 정의하지만이 스타일을 사용하는 곳에서는 내 바인딩 된 데이터의 이름이 같지 않을 수 있으므로 경로를 설정하지 않습니다.스타일을 사용하지만 경로가 아닌 변환기를 설정하는 방법은 무엇입니까?
<Style x:Key="CustomTextBox" BasedOn="{StaticResource {x:Type TextBox}}"
TargetType="{x:Type TextBox}">
<Setter Property="Text">
<Setter.Value>
<Binding>
<Binding.Converter>
<CustomTextBoxConverter/>
</Binding.Converter>
</Binding>
</Setter.Value>
</Setter>
</Style>
그리고 다음 customTextBox은과 같이 사용된다 :
<TextBox Height="28" Name="txtRate" Style="{StaticResource CustomTextBox}"
MaxLength="5" Text="{Binding Path=BoundData}"/>
나는 위의 코드를 작성할 때, 나는 있음을 execption 얻을 "바인딩 두 방법은 경로 또는 XPath를 필요로합니다.".
스타일에이 값을 반영하기 위해 스타일 바인딩에 사용되는 첨부 된 속성을 만들려고했지만 작업 할 수도 없습니다. 다음을 참조하십시오 :
<Converters:SomeConvertingFunction x:Key="CustomTextConverter"/>
<local:CustomAttachedProperties.ReflectedPath x:Key="ReflectedPath"/>
<Style x:Key="CustomTextBox" BasedOn="{StaticResource {x:Type TextBox}}"
TargetType="{x:Type TextBox}">
<Setter Property="Text">
<Setter.Value>
<Binding Path=ReflectedPath Converter=CustomTextConverter/>
</Setter.Value>
</Setter>
</Style>
그래서 같은 페이지에서 사용 :
이
<TextBox Height="28" Name="txtRate" Style="{StaticResource CustomTextBox}"
MaxLength="5" CustomAttachedProperty="contextBoundDataAsString"/>
연결된 속성에 대한 코드는 : 나는 위의 코드를 사용하려고하면
Public Class CustomAttachedProperties
Public Shared ReadOnly ReflectedPathProperty As DependencyProperty =
DependencyProperty.RegisterAttached("ReflectedPath", GetType(String),
GetType(CustomAttachedProperties))
Public Shared Sub SetReflectedPath(element As UIElement, value As String)
element.SetValue(ReflectedPathProperty, value)
End Sub
Public Shared Function GetReflectedPath(element As UIElement) As String
Return TryCast(element.GetValue(ReflectedPathProperty), String)
End Function
End Class
가 잘 컴파일 그러나 CustomAttachedProperty의 다른 인스턴스를 만드는 것처럼 XAML에서 아무 것도하지 않는 것처럼 보입니다.
불충분 한 질문에 대해 사과했지만 WPF를 사용하는 자체 기본 변환기가있는 사용자 지정 컨트롤을 만드는 것이 쉬워야한다고 생각했습니다. 혼란 스럽습니다!
아마도 하드 코딩 된 경로를 스타일에 넣을 수는 있지만 그 스타일은 스타일의 목적을 상실 할 수 있습니다. – Paparazzi
이 * 매우 * 비슷한 SO 스레드를보고 싶을 수도 있습니다. http://stackoverflow.com/questions/378979/is-it-possible-to-use-a-converter-within-a-style – superjos