简体   繁体   中英

How do I set up a two-way binding to ancestor properties in a UserControl in Windows Phone 7?

Here is the XAML for my user control:

<UserControl x:Name="titledTextBox" x:Class="VitalStats.View.Controls.TitledTextBox"
    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"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="480" d:DesignWidth="480"
    >

    <Grid x:Name="LayoutRoot">
        <StackPanel Orientation="Vertical">
            <TextBlock x:Name="titleTextBlock" 
                       TextWrapping="Wrap" 
                       Margin="12,5,0,-5" 
                       Text="{Binding Title, ElementName=titledTextBox, FallbackValue=Title Here}" 
                       FontSize="20" 
                       Foreground="{StaticResource PhoneSubtleBrush}"/>
            <TextBox x:Name="inputTextBox" Text="{Binding Text, ElementName=titledTextBox, Mode=TwoWay}"/>   
        </StackPanel>
    </Grid>

</UserControl>

and this is what my code-behind looks like,

// Usings here

namespace VitalStats.View.Controls
{
    public partial class TitledTextBox : UserControl
    {

        [Description("A TextBox with built in title")]
        public TitledTextBox()
        {
            InitializeComponent();

            if (DesignerProperties.GetIsInDesignMode(this)  )
            {
                this.Title = "Title Here";  
            }
        }

        public string Title
        {
            get { return this.GetValue(TitleProperty) as string; }
            set { this.SetValue(TitleProperty, value); }
        }
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(TitledTextBox), null);

        public string Text
        {
            get { return this.GetValue(TextProperty) as string; }
            set { this.SetValue(TextProperty, value); }
        }
        public static readonly DependencyProperty TextProperty = 
            DependencyProperty.Register("Text", typeof(string), typeof(TitledTextBox), null);

    }
}

The binding work when reading data into the UI (so the Title property works OK) however when reading from th UI (ie trying to access Text from the code) the property is always null implying that the binding is only one-way (despite the Mode=TwoWay property).

I am aware (thanks to XamlZealot's answer) of the FindAncestor binding, however AncestorType does not exist in Windows Phone 7 (or Silverlight) XAML namespace.

How then do I set up a two-way binding to a UserControl property from within the UserControl ?

This is on Windows Phone 7 (7.1 project).

尝试使用RelativeSource绑定代替ElementName

Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=Title, FallbackValue=Title Here}"

So the following is perhaps not what I asked but what I used to solve the problem. Since all I wanted to do was link the text of the title TextBlock to Title on the UserControl and the same for the text in the input, the code behind can look like this,

public string Title
{
    get { return this.titleTextBlock.Text; }
    set { this.titleTextBlock.Text = value; }
}

public string Text
{
    get { return this.inputTextBox.Text; }
    set { this.inputTextBox.Text = value; }
}

No need for DependencyProperties etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM