【问题标题】:Silverlight for windows phone 7 custom textbox usercontrol text property returning nullSilverlight for windows phone 7 自定义文本框用户控件文本属性返回 null
【发布时间】:2011-07-07 10:38:19
【问题描述】:

我正在为 Windows Phone 7 制作应用程序,但遇到了以下问题: 当我创建一个包含文本框的用户控件,并使 DependencyProperty 能够对其进行数据绑定时,用于访问它的属性返回 null,并且当我尝试将文本设置为某些内容时,它不会这样做。我读到Here 说“您需要在设置器中调用 NotifyPropertyChanged() 来获取要绑定的项目。”,但我不知道该怎么做。有 INotifyPropertyChanged 接口,但我认为我不需要它,而且我没有找到任何线索如何编写它,如果我必须编写它。

这是我的用户控件:

    <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"
             BorderThickness="0"
             IsReadOnly="{Binding IsReadOnly}"
             Text="{Binding TextContent}">
    </TextBox>
</Grid>

这里是 DependencyProperty:(编辑:重新格式化)

    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:我知道样式和模板,但由于我对更复杂的控件也有同样的问题,所以这不是一个选择。

编辑: 我解决了这个问题,解决了“文本不显示”问题,我可以将文本框的文本放在另一个控件上,但是当我对其进行数据绑定时,它仍然不起作用。我通过编写 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());
}

以及文本框的 TextChanged 事件处理程序:

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

【问题讨论】:

    标签: silverlight windows-phone-7 dependency-properties


    【解决方案1】:

    在初始化组件this.DataContext = this;下设置datacontext希望对你有帮助

    【讨论】:

    • DataContext="{Binding RelativeSource={RelativeSource Self}}" 已经完成了。
    • 我说一开始就搞定了,但是2天后,我和我的一个朋友想通了,我只是还不能发。
    • 哇哦,我把它放在这里将近 2 年了。事实证明,我们毕竟没有弄清楚,所以没有知识丢失。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 2013-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    相关资源
    最近更新 更多