Windows Phone 7 XAML —使绑定与我的对象的容器一起使用

|| 我想要做的是将TextBlock的文本绑定到UserControl的自定义ButtonSymbol属性。 这是UserControl的XAML。 TextBlock的Binding部分需要填写。
<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=\"buttonb@2x.png\" 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>
这是背后的代码:
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.
        }
    }
}
请注意,这是带有Silverlight的WP7,并且RelativeSource类与其他版本不同。     
已邀请:
您需要设置用户控件的DataContext。 如果添加此:
this.DataContext = this;
然后,您可以将其放入用户控件的构造函数或Loaded事件中:
Text=\"{Binding ButtonSymbol}\"
请注意,您还可以声明性地绑定XAML的数据源,这只是一种简单的编程方式。     

要回复问题请先登录注册