2012-06-20 1 views
3

내 코드 전체에서 사용할 수있는 텍스트 상자의 스타일을 만들려고합니다. 내 스타일은 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를 사용하는 자체 기본 변환기가있는 사용자 지정 컨트롤을 만드는 것이 쉬워야한다고 생각했습니다. 혼란 스럽습니다!

+0

아마도 하드 코딩 된 경로를 스타일에 넣을 수는 있지만 그 스타일은 스타일의 목적을 상실 할 수 있습니다. – Paparazzi

+0

이 * 매우 * 비슷한 SO 스레드를보고 싶을 수도 있습니다. http://stackoverflow.com/questions/378979/is-it-possible-to-use-a-converter-within-a-style – superjos

답변

1

당신이 매우 간단하게 수행하는 UserControl 만들 수 있습니다 : 다음의 DependencyPropertyText를 선언

<UserControl x:Class="namespace.MyCustomConverterTextBox"> 
    <TextBlock Text="{Binding Text, Converter={StaticResource yourconverter}}"/> 
</UserControl> 

을 코드 숨김 :

public partial class MyCustomConverterTextBox : UserControl 
{ 
    public string Text { 
     get{return GetValue(TextProperty);} 
     set{SetValue(TextProperty, value);} 
    } 

    public static readonly DependencyProperty TextProperty = 
     DependencyPropery.Register("Text", typeof(string), typeof(MyCustomConverterBox)); 
} 

것은 이것은 당신이 그것을 사용하게하기에 충분합니다 당신의 xaml :

<local:MyCustomConverterTextBox Text="{Binding YourBinding}" /> 

나는 는 코드이므로 오타가있을 수 있지만이 문제를 해결하는 방법을 알기에는 충분해야합니다.

1

내 의견으로는이 작업을 수행하는 가장 좋은 방법은, 다음 자신에게 Coerce 콜백에서 값을 변경 할 수있는 기회를 허용하는 Text 속성의 PropertyMetadata를 오버라이드 (override), TextBox에서 상속하는 새로운 클래스를 만드는 것입니다.

관련 문제