WP7绑定到自定义用户控件中的属性的问题

时间:2012-03-19 01:08:47

标签: windows-phone-7 user-controls

所以我正在尝试为我称之为ColoredTextBlock的Windows Phone 7应用程序进行自定义用户控件。你可能猜到它的作用。

无论如何,ColoredTextBlock包含一个TextBlock,我希望用户能够为其设置Text和Style。

如果我尝试制作一个刚刚通过的简单属性,例如:

    public string Text
    {
        get { return Label.Text; }
        set
        {
            Label.Text = value;
            NotifyPropertyChanged("Text");
        }
    }

它会导致非常神秘的ArgumentException。但是,如果我设置输入文本,例如:

 <MyRepresentative:ColoredTextBlock Text="Some Text" BackgroundColor="Red" />

一切都按照我的预期完成。

另一方面,如果我使用更高级的路径使用Dependency属性并将Inner TextBlock绑定到此属性,然后将外部数据绑定到此属性,则不显示任何内容。

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set
        {
            SetValue(TextProperty, value);
            NotifyPropertyChanged("Label");
        }
    }

    public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register("Text", typeof(string), typeof(ColoredTextBlock), null);

但是,如果我手动插入文本,整个过程就可以了。

这是我的自定义控件的xaml:

 <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="MyRepresentative.ColoredTextBlock"
d:DesignWidth="456" d:DesignHeight="43"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="{Binding DimBackgroundColor}" Offset="0"/>
                    <GradientStop Color="{Binding BrightBackgroundColor}" Offset="0.85"/>
                    <GradientStop Color="{Binding BrightBackgroundColor}" Offset="0.15"/>
                    <GradientStop Color="{Binding DimBackgroundColor}" Offset="1"/>
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>
        <TextBlock Text="{Binding Text}" Margin="5,0" d:LayoutOverrides="Width"/>
    </Grid>
 </UserControl>

我在这一天的大部分时间里一直绞尽脑汁,看着这么多不同的文章,我肯定在这一点上我错过了一些小事,但我不能找到它。

更新1 :进一步研究后,看起来由于某种原因,即使我用绑定设置它,它似乎并没有实际设置,至少到目前为止我可以告诉你。

更新2 :根据评论,您询问了如何确保我的DataContext设置正确。雅,这是我想到的第一件事。我的xaml下面有一行。

 <MyRepresentative:ColoredTextBlock Text="{Binding Title}" BackgroundColor="Red" />
 <TextBlock Text="{Binding Title}" Style="{StaticResource PhoneTextLargeStyle}" />

所以第一个元素没有显示(根本没有),除非我改为像Text="Some text"这样的东西。第二个元素完美无误地运行。

1 个答案:

答案 0 :(得分:0)

我自己最终弄清楚了这个问题的答案,虽然我花了一段时间,并且有点搜索。答案实际上是受到this answer的启发。

<强>答案:

而不是使用:

绑定到根元素
 DataContext="{Binding RelativeSource={RelativeSource Self}}" 

就像我在上面的例子中所做的那样:

 <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="MyRepresentative.ColoredTextBlock"
     d:DesignWidth="456" d:DesignHeight="43"
     DataContext="{Binding RelativeSource={RelativeSource Self}}">

绑定到LayoutRoot,在本例中为Grid Element。

 <Grid x:Name="LayoutRoot">

这可以在你的代码后面,在构造函数中完成,就像我在这里做的那样:

    public ColoredTextBlock()
    {
        ...
        LayoutRoot.DataContext = this;
        ...
    }

<强>解释

我不完全理解为什么这是诚实的,如果有人可以解释,请编辑。

当您绑定到根元素时,它似乎与覆盖现有绑定有关。因此,当您尝试将外部元素绑定到此控件的元素时,它将失败,因为无法访问该绑定。

希望能帮助遇到与我相同问题的其他人!