2011-04-25 2 views
1

내가 원하는 것은 TextBlock의 텍스트를 UserControl의 사용자 지정 ButtonSymbol 속성에 바인딩하는 것입니다.Windows Phone 7 XAML - 내 개체 컨테이너에서 작동하도록 바인딩 가져 오기

다음은 UserControl의 XAML입니다. . TextBlock에 대한 바인딩 부분에 채워 져야

<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    x:Class="Calculator.CalculatorButton" 
    d:DesignWidth="120" d:DesignHeight="80"> 

    <Grid x:Name="LayoutRoot" Background="Transparent"> 
     <Image Source="[email protected]" Stretch="Fill"/> 
     <Button x:Name="InvisibleButton" Content="{Binding ButtonSymbol}" Margin="0,0,0,0" d:LayoutOverrides="Width, Height" BorderThickness="1" Click="InvisibleButton_Click"/> 
    <TextBlock HorizontalAlignment="Center" Margin="0,0,0,0" TextWrapping="Wrap" 
       Text="{Binding ????????}" 
       VerticalAlignment="Top"/> 
    </Grid> 
</UserControl> 

을 필요로 그리고 여기에 코드 숨김입니다 :이 WP7 실버 라이트와 함께, 그리고 RelativeSource class 다른에서와 동일하지 않습니다

namespace Calculator 
{ 
    public partial class CalculatorButton : UserControl 
    { 
     public string ButtonSymbol {get; set;} 

     public CalculatorButton() 
     { 
      // Required to initialize variables 
      InitializeComponent(); 
     } 

     private void Button_Click(object sender, System.Windows.RoutedEventArgs e) 
     { 
      // TODO: Add event handler implementation here. 
     } 

     private void InvisibleButton_Click(object sender, System.Windows.RoutedEventArgs e) 
     { 
      Debug.WriteLine(@"click"); 
      Debug.WriteLine(ButtonSymbol); 
      // TODO: Add event handler implementation here. 
     } 
    } 
} 

주 버전.

답변

3

사용자 정의 컨트롤의 DataContext를 설정해야합니다. 이 추가하면

: 사용자 정의 컨트롤의 생성자 또는로드 이벤트에

this.DataContext = this; 

당신은이 작업을 수행 할 수 있습니다 당신은 또한 선언적으로의 데이터 소스를 바인딩 할 수 있습니다

Text="{Binding ButtonSymbol}" 

주 XAML,이 작업은 프로그래밍 방식으로 간단합니다.