2012-09-28 1 views
1

DependencyProperty을 내 맞춤 검색 버튼의 라벨에 바인딩하고 싶습니다. 여기 Label의 Text 속성에 DependencyProperty를 바인딩 하시겠습니까?

DependencyProperty입니다 : 다른 경우지만 여러 번했다

<TextBlock x:Name="lblButtonText" Margin="16,45.2465" TextWrapping="Wrap" Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=ButtonText}" VerticalAlignment="Center" TextAlignment="Right" Foreground="White" FontSize="17.333" FontWeight="Bold" FontFamily="Segoe UI Semibold" Height="26.053"/> 

내 DependencyProperties 바인딩의이 방법 : 여기

public static readonly DependencyProperty ButtonTextProperty = DependencyProperty.Register("ButtonText", typeof(string), typeof(MainMenuButton)); 

public string ButtonText 
    { 
     get { return (string)GetValue(ButtonTextProperty); } 
     set { SetValue(ButtonTextProperty, value); } 
    } 

그리고 것은 라벨에 값을 바인딩하는 내 시도 이번엔 왜 작동하지 않는지 알 수 없습니까?

이 문제를 어떻게 해결할 수 있습니까?

+1

RelativeSource 포인트 그것 자체 ... 그래서 ButtonText라는 TextBlock의 속성에 바인딩하려고합니다 – Johnny

+0

Thanks Johnny :) – TorbenJ

답변

1
당신은 당신의 바인딩 Self의 소스를 설정하는

SelfTextBlock이며, TextBlock는 속성 ButtonText

당신은 아마 당신이의 RelativeSource에 바인딩의 소스를 설정하기 위해 찾고라고하지 않습니다 MyCustomButton 또는

Text="{Binding Path=ButtonText, 
    RelativeSource={RelativeSource AncestorType={x:Type local:MyCustomButton}}}" 
+0

고마워요 :) 데이터 바인딩을 이해하는 또 다른 단계. – TorbenJ

1

이 같은 결합을 시도 할 수 입력 :

먼저 당신의 고객 사양을 제공 m 제어 이름 :

<uc:myControl x:Name="ucName" ButtonText="MyText" .../> 

<Window xmlns:uc="NamespaceToMyUc" ...> 

(이 예에서는 UC 불리는의 xmlns를 가져와야) 다음에 사용하여 바인딩 ElementName을 :

<TextBlock Text="{Binding ElementName=ucName, Path=ButtonText} ... /> 
관련 문제