【发布时间】:2012-03-19 01:08:46
【问题描述】:
所以我正在尝试为 Windows Phone 7 应用程序创建一个自定义用户控件,我称之为 ColoredTextBlock。你大概可以猜到它的作用。
无论如何,ColoredTextBlock 包含一个 TextBlock,我希望用户能够为其设置文本和样式。
如果我尝试只创建一个简单的属性,例如:
public string Text
{
get { return Label.Text; }
set
{
Label.Text = value;
NotifyPropertyChanged("Text");
}
}
它会导致一个非常神秘的 ArgumentException。但是,如果我设置输入文本,例如:
<MyRepresentative:ColoredTextBlock Text="Some Text" BackgroundColor="Red" />
一切都如我所料。
另一方面,如果我采用更高级的方法,即使用 Dependency 属性并将内部 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" 之类的东西。第二个元素完美无误。
【问题讨论】:
-
您没有包含设置 DataContext 的代码(以便您的 {Binding} 工作)。另外,您是否通知了错误的属性名称? (您在名为“Text”的属性上调用 NotifyPropertyChanged("Label"))
-
嘿Shahar,我试图在上面的更新中解决您的评论。名称的问题是因为我尝试了许多不同的方法来让它发挥作用。但是,我确实确保它们现在是一致的。
-
我的意思是您的数据上下文在您的控制范围内。您在顶部引用自己 - 但在您的示例代码中,您绑定到“标题”并且在控件中您绑定到“文本”
-
因此具有实际文本的 ViewModel 元素是 Title。我将它绑定到我的 UserControl 上的 Text 属性,并且我还将 TextBlock 的 Text 属性绑定到我的 UserControl 的 Text 属性。希望能解决一些困惑。
-
Gotcha - 所以这可能只是我在这里缺乏理解......你在那里的RelativeSource Self本质上指向控件的DataSource(而不是this [控件实例] 将拥有你的属性)?通过阅读RelativeSource,这就是我所收集的。您是否尝试将控件中的绑定更改为 {Title} 只是为了看看它是否有效?
标签: windows-phone-7 user-controls