开发者

Silverlight for windows phone 7 custom textbox usercontrol text property returning null

I am making an application for Windows Phone 7, and i ran into the following problem: When I make a usercontrol that contains a textbox, and make a DependencyProperty to be able to databind it, the property for accessing it returns null, and when i'm triing to set the text to something, it doesn't do it. I read Here that "You need to call NotifyPropertyChanged() in your setters for the items you wish to bind to.", but I don't know how to do that. There is the INotifyPropertyChanged interface, but i dont think I need that, and I didn't find any clues how to write it, if I had to write it.

Here's my usercontrol:

    <Grid VerticalAlignment="Stretch">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"></ColumnDefinition>
        <ColumnDefinition Width="5.18*"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="1*"></RowDefinition>
        <RowDefinition Height="1*"></RowDefinition>
    </Grid.RowDefinitions>

    <TextBlock VerticalAlignment="Bottom"
               HorizontalAlignment="Left"
               Grid.Column="1"
               Grid.Row="0"
               Text="{Binding Tag}"
               Foreground="Black"
               Margin="10,0,0,0"
               Style="{StaticResource MenuTextStyle}"
               TextWrapping="Wrap"/>
    <Image Source="../Resources/Graphics/sign_arrow_black.png"
           Margin="0,15,0,0"
           HorizontalAlignment="Right"
           VerticalAlignment="Top"
           Grid.Column="0"
           Grid.Row="1"
           Height="30">
    </Image>
    <TextBox x:Name="ContentBox"
             Style="{StaticResource TextBoxStyle}"
             VerticalAlignment="Top"
             HorizontalAlignment="Stretch"
             HorizontalContentAlignment="Left"
             VerticalContentAlignment="Center"
             Grid.Column="1"
             Grid.Row="1"
             Padding="0,0,0,0"
             Margin="0,0,0,0"
             BorderThickne开发者_JAVA技巧ss="0"
             IsReadOnly="{Binding IsReadOnly}"
             Text="{Binding TextContent}">
    </TextBox>
</Grid>

And here's the DependencyProperty: (EDIT: Reformatted)

    public static readonly DependencyProperty TextContentProperty = DependencyProperty.Register("TextContent", typeof(string), typeof(LargeTextBox), new PropertyMetadata(default(string)));

    public string TextContent
    {
        get { return (string)GetValue(TextContentProperty); }
        set { SetValue(TextContentProperty, value);}
    }

PS: I know about styles, and templates, but since i have the same problem with more complex controls, it's not an option.

EDIT: I worked on the problem, and solved the "text not showing"-issue, and i could get the textboxt's text put on another control, but when i'm databinding it, it still doesn't work. I achieved this by writing the ValueChangedCallback:

private static void OnTextPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    LargeTextBox box = (LargeTextBox)sender;
    box.ContentBox.Text = box.Text;
    if (box.TextChanged != null) box.TextChanged(box, new DependencyPropertyChangedEventArgs());
}

And the textbox's TextChanged eventhandler:

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        Text = ContentBox.Text;
    }


Set datacontext under initialize component this.DataContext = this; Hope that helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜