Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows phone 7 WP7绑定到自定义用户控件中的属性时出现的问题_Windows Phone 7_User Controls - Fatal编程技术网

Windows phone 7 WP7绑定到自定义用户控件中的属性时出现的问题

Windows phone 7 WP7绑定到自定义用户控件中的属性时出现的问题,windows-phone-7,user-controls,Windows Phone 7,User Controls,因此,我正在尝试为WindowsPhone7应用程序创建一个自定义用户控件,我称之为ColoredTextBlock。你大概可以猜到它的作用 无论如何,ColoredTextBlock包含一个TextBlock,我希望用户能够为其设置文本和样式 如果我尝试创建一个简单的属性,该属性正好通过,例如: public string Text { get { return Label.Text; } set { La

因此,我正在尝试为WindowsPhone7应用程序创建一个自定义用户控件,我称之为ColoredTextBlock。你大概可以猜到它的作用

无论如何,ColoredTextBlock包含一个TextBlock,我希望用户能够为其设置文本和样式

如果我尝试创建一个简单的属性,该属性正好通过,例如:

    public string Text
    {
        get { return Label.Text; }
        set
        {
            Label.Text = value;
            NotifyPropertyChanged("Text");
        }
    }
它导致了一个非常隐晦的异常。但是,如果我设置输入文本,例如:

 <MyRepresentative:ColoredTextBlock Text="Some Text" BackgroundColor="Red" />
不过,如果我手动插入文本,整个过程就会正常工作

以下是我的自定义控件的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”
。第二个元素可以完美地工作,没有错误。

我在做与自己相同的事情时遇到了一些真正的麻烦,没有运气,我找到的唯一条目谈到这种方法不起作用(很抱歉,我再也找不到了)

如果我通过C#设置绑定,我确实成功了。我举了一个例子


编辑:我刚刚找到的另一个解决方案是使用。

我最终自己找到了这个问题的答案,尽管这花了我一段时间,也花了我一点时间搜索。答案实际上是受到了他的启发

回答:

而不是使用以下命令绑定到根元素:

 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}}">
说明:

老实说,我不完全理解为什么这样做有效,如果有人能解释,请编辑

这似乎与绑定到根元素时重写现有绑定有关。因此,当您尝试将外部元素绑定到此控件的元素时,它将失败,因为该绑定不可访问


希望这能帮助其他遇到和我一样问题的人

您没有包含设置DataContext(使{Binding}工作)的代码。另外,您是否通知了错误的财产名称?(你在一个名为“文本”的属性上调用NotifyPropertyChanged(“标签”)嘿,Shahar,我试图在上面的更新中回应你的评论。名字的问题是因为我尝试了很多不同的方法来让它发挥作用。不过,我现在确实确保了它们的一致性。我指的是控制范围内的datacontext。您在顶部引用自己-但是在您的示例代码中,您绑定到“Title”,在控件中绑定到“Text”,因此具有实际文本的ViewModel元素是Title。我将它绑定到UserControl上的Text属性,并且我将TextBlock的Text属性绑定到UserControl的Text属性。希望这能解决一些困惑。明白了-所以这可能是我在这里缺乏理解。。。您拥有的RelativeSource Self是否本质上指向控件的数据源(与此[控件实例]相反,它将拥有您的属性)?通过阅读有关RelativeSource的内容,这就是我收集的信息。您是否尝试将控件中的绑定更改为{Title}以查看它是否有效?谢谢您的建议。我都查过了。不幸的是,他们似乎没有给我比我现在得到的更好的结果:(
 <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">
    public ColoredTextBlock()
    {
        ...
        LayoutRoot.DataContext = this;
        ...
    }