Windows Phone 7的XAML - 获得一个绑定和我对象的容器中工作 [英] Windows Phone 7 XAML -- getting a binding to work with the container of my object

查看:170
本文介绍了Windows Phone 7的XAML - 获得一个绑定和我对象的容器中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是TextBlock的文本绑定到用户控件的我的自定义ButtonSymbol属性是什么。

What I want to do is bind the text of a TextBlock to my custom ButtonSymbol property of the UserControl.

下面是用户控件的XAML。需要被填充的TextBlock的结合部。

Here is the XAML for the UserControl. The Binding part for the TextBlock needs to be filled in.

<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>

这里是codeBehind:

And here is the CodeBehind:

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.
        }
    }
}

请注意,这是WP7与Silverlight和的的RelativeSource类是不一样的其他版本。

Note that this is WP7 with Silverlight, and the RelativeSource class is not the same as in other versions.

推荐答案

您需要设置用户控件的DataContext的。

You need to set the DataContext of the user control.

如果你补充一点:

this.DataContext = this;

到用户的控制,那么你可以做到这一点的构造函数或加载的事件:

into the constructor or Loaded event of the user control you can then do this:

Text="{Binding ButtonSymbol}"

请注意,您还可以声明绑定XAML的数据源,这只是一个简单的程序化的方法来做到这一点。

Note that you can also declaratively bind the DataSource of the XAML, this is just an easy programmatic way to do it.

这篇关于Windows Phone 7的XAML - 获得一个绑定和我对象的容器中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆