【问题标题】:databinding on a user control only working partially (silverlight)用户控件上的数据绑定仅部分工作(silverlight)
【发布时间】:2011-09-03 16:25:12
【问题描述】:

我不确定我在这里做错了什么。昨晚我花了一个小时才弄清楚,也许我只是愚蠢。

我创建了这个用户控件来显示一个有边框的文本,它使用数据绑定来填充样式和文本。

这就是我从主页上调用它的方式:

<mynamespace:BorderedText x:Name="DateTime"
                   Grid.Column="1"
                   Grid.Row="0"
                   BorderStyle="{StaticResource borderStyle}"
                   LabelStyle="{StaticResource labelStyle}"
                   TextStyle="{StaticResource valueStyle}"
                   Label="Current Date/Time"                                           
                   Text="N/A" />

控制很简单:

<UserControl x:Class="MyNamespace.BorderedText"
         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"
         d:DesignHeight="480"
         d:DesignWidth="480">

<Grid>
    <Border Name="border" Style="{Binding BorderStyle}">
        <StackPanel>
            <TextBlock Style="{Binding LabelStyle}"
                       Text="{Binding Label}" />
            <TextBlock Style="{Binding TextStyle}"
                       Text="{Binding Text}" />
        </StackPanel>
    </Border>
</Grid>

问题是所有数据绑定都有效,除了边界数据绑定。我还尝试对背景或任何其他属性进行数据绑定,但没有成功。 后面的代码设置了 DependencyProperty 属性,仅此而已。请注意,数据绑定的 DataContext 是在构造函数中设置的。试图将其分配给 Grid 或 Border 本身,但没有成功。 有没有人有任何线索或看到我在这里遗漏的重要内容?

namespace MyNamespace
{
    public partial class BorderedText : UserControl
    {
        public static readonly DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(BorderedText), new PropertyMetadata(null));

        public static readonly DependencyProperty LabelStyleProperty = DependencyProperty.Register("LabelStyle", typeof(Style), typeof(BorderedText), new PropertyMetadata(null));

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

        public static readonly DependencyProperty TextStyleProperty = DependencyProperty.Register("TextStyle", typeof(Style), typeof(BorderedText), new PropertyMetadata(null));

        public static readonly DependencyProperty BorderStyleProperty = DependencyProperty.Register("BorderStyle", typeof(Style), typeof(BorderedText), new PropertyMetadata(null));

        public BorderedText()
        {
            InitializeComponent();
            ((Grid)this.Content).DataContext = this;
            //((Border)this.Content).DataContext = this;
        }

        public string Label
        {
            set { SetValue(LabelProperty, value); }
            get { return (string)GetValue(LabelProperty); }
        }

        public Style LabelStyle
        {
            set { SetValue(LabelStyleProperty, value); }
            get { return (Style)GetValue(LabelStyleProperty); }
        }

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

        public Style TextStyle
        {
            set { SetValue(TextStyleProperty, value); }
            get { return (Style)GetValue(TextStyleProperty); }
        }

        public Style BorderStyle
        {
            set { SetValue(BorderStyleProperty, value); }
            get { return (Style)GetValue(BorderStyleProperty); }
        }
    }
}

---- 更新:

事实证明这是完全不同的东西,与正确连接的数据绑定无关......

在borderStyle 中,我将这种语法用于背景属性:

        <Setter Property="Background">
            <Setter.Value>
                <SolidColorBrush>
                    <SolidColorBrush.Color>
                        <Color>
                            <Color.A>
                                100
                            </Color.A>
                            <Color.R>#95</Color.R>
                            <Color.B>#ED</Color.B>
                        </Color>
                    </SolidColorBrush.Color>
                </SolidColorBrush>
            </Setter.Value>
        </Setter>

这显然适用于设计器,但不适用于手机。 将其更改为:

        <Setter Property="Background">
            <Setter.Value>
                <SolidColorBrush Color="#649500ED" />
            </Setter.Value>
        </Setter>

解决了问题

【问题讨论】:

    标签: silverlight data-binding user-controls


    【解决方案1】:

    好吧,你忘了一件事……边框的 DataContext! 给你的 UserControl 一个名字,然后你可以在你的绑定中添加如下内容:

    <TextBox Text="{Binding Path=MyText, ElementName=UserControlRoot}" />
    

    这会起作用(至少它在 WPF 中对我有用,呵呵)

    【讨论】:

    • 边框的 DataContext 应该在用户控件的初始化程序中定义(但无论是注释的还是未注释的都不起作用)。给用户控件一个特定的名称是行不通的,因为我需要在调用页面中多次重用用户控件。如果我在用户控件中指定一个特定的名称,当我将 x:Name 分配给调用者中的控件时,数据绑定会再次中断。
    • 嗯,我不确定我是否关注你.. 为什么 UserControl 中的名称会影响它之外的任何内容?无论如何,如果你不想要一个名字,你可以将你的边框/文本块绑定到RelativeSource Find Ancestor type UserControl。此外,初始化程序中的数据上下文似乎不正确。你是说控件是一个网格,而它不是。你可以这样做: DataContext = this; (我认为,没有测试)
    • 如果你在外面用 命名用户控件,会发生什么情况是该名称会覆盖分配给对象的内部名称。因此 Binding ElementName="nameassignedinternally" 中断。您可以在设计器中看到这种情况。
    • 另外,该控件有一个包含边框的网格。无论如何,结果完全不同。请参阅上述问题中的更新。解决了!
    猜你喜欢
    • 2013-03-30
    • 2023-03-24
    • 2010-12-20
    • 2011-02-03
    • 2010-11-18
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多