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

发布于 2024-11-03 07:43:05 字数 2118 浏览 0 评论 0原文

我想要做的是将 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="[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>

这里是 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.
        }
    }
}

请注意,这是带有 Silverlight 的 WP7,并且 RelativeSource 类 与其他版本中的不同。

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

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="[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>

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻泪 2024-11-10 07:43:05

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

如果将以下内容添加

this.DataContext = this;

到用户控件的构造函数或 Loaded 事件中,则可以执行以下操作:

Text="{Binding ButtonSymbol}"

请注意,您还可以以声明方式绑定 XAML 的数据源,这只是一种简单的编程方法。

You need to set the DataContext of the user control.

If you add this:

this.DataContext = this;

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

Text="{Binding ButtonSymbol}"

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文