Silverlight 中的 DataContext、DataBinding 和元素绑定

发布于 2024-08-26 08:17:03 字数 1472 浏览 5 评论 0原文

我正在努力让我的数据绑定正常工作。我有理由相信我想要完成的事情无法完成,但我们会看看我得到什么答案。

我有一个用户控件。该用户控件仅包含一个按钮。现在在后面的代码中,我有一个属性名称 IsBookmarked。设置 IsBookmarked 后,将运行代码来为按钮的外观添加动画效果。这个想法是,您单击按钮,它的视觉效果就会发生变化。我们将此 UserControl 称为 Bookmark 控件。

现在我有另一个控件,我们将其称为 FormControl。我的 FormControl 包含一个子 Bookmark 控件。我尝试在我的 Bookmark 控件上进行数据绑定,但它不起作用。这里有一些代码可以帮助您。

这是我的控件的 XAML 和 Loaded 事件处理程序。正如您所看到的,它包含一个作为自定义控件(书签)的子元素。因此,一旦加载此控件,它的 DataContext 就会设置为 Employee 对象的新实例。 Silverlight 还将我的子书签控件的 DataContext 属性设置为同一个实例。我已经通过调试验证了这一点。如果我的父级设置了有效的 DataContext,那么为什么我的子级控件(书签)属性数据不能绑定到它?

<UserControl ......>
    <q:Bookmark x:Name="BookMarkControl1" IsBookmarked="{Binding IsSiteBookmarked}" />
</UserControl>

public void Control_Loaded(object sender, EventArgs e)
{
    DataContext = new Employee { IsSiteBookmarked = True };

}

这是我下面的自定义控件。显然它包含的内容不止于此,但为了便于阅读,我将其精简为我尝试进行数据绑定的属性。

//this is the bookmark control. I've included this control within another control, and I'm trying to databind to properties within my parents DataContext
public partial class Bookmark : UserControl
{

    bool _IsBookmarked= false;
    public bool IsBookmarked
    {
        get {return _IsBookmarked;}
        set { 
                 _IsBookmarked= value;
                 SwitchMode(value);
            }
    }

}

更新

有一些我应该提到的 javascript 错误。 Firebug 报告 AG_E_PARSER_BAD_PROPERTY_VALUE 异常。我的数据绑定似乎还没有起作用。

I'm having one hell of a time trying to get my databinding to work correctly. I have reason to believe that what I'm trying to accomplish can't be done, but we'll see what answers I get.

I've got a UserControl. This UserControl contains nothing more than a button. Now within the code behind, I've got a property name IsBookmarked. When IsBookmarked is set, code is run that animates the look of the button. The idea is that you click the button and it visually changes. We'll call this UserControl a Bookmark control.

Now I have another control, which we'll call the FormControl. My FormControl contains a child Bookmark control. I've tried to do databinding on my Bookmark control, but it's not working. Here's some code to help you out.

This is the XAML and Loaded event handler of my control. As you can see it contains a child element that is a custom control (bookmark). So once this control loads, it's DataContext is set to an new instance of an Employee object. Silverlight also sets the DataContext property of my child bookmark control to the same instance. I've verified this by debugging. If my parent has a valid DataContext set then why can't my child control (bookmark) property databind to it?

<UserControl ......>
    <q:Bookmark x:Name="BookMarkControl1" IsBookmarked="{Binding IsSiteBookmarked}" />
</UserControl>

public void Control_Loaded(object sender, EventArgs e)
{
    DataContext = new Employee { IsSiteBookmarked = True };

}

This is my custom control below. Obviously it contains more than this, but for readability I've trimmed it down to the property I'm trying to databind to.

//this is the bookmark control. I've included this control within another control, and I'm trying to databind to properties within my parents DataContext
public partial class Bookmark : UserControl
{

    bool _IsBookmarked= false;
    public bool IsBookmarked
    {
        get {return _IsBookmarked;}
        set { 
                 _IsBookmarked= value;
                 SwitchMode(value);
            }
    }

}

UPDATE

Got some javascript errors that I should mention. Firebug reports a AG_E_PARSER_BAD_PROPERTY_VALUE exception. It doesn't seem like my databinding is even working yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眉黛浅 2024-09-02 08:17:03

将 Bookmark 控件上的 IsBookmarked 属性设置为依赖属性。

我认为 Control_Loaded 是 FormControl 的一部分,在这种情况下,我不确定您是否正确使用 DataContext。最好仔细检查一下。

更新:是的,您正在正确使用 DataContext。 AG_E_PARSER_BAD_PROPERTY_VALUE 表示您需要将 IsBookmarked 属性设置为依赖属性,如下所示:

 Public Property IsBookmarked() As Boolean
    Get
        Return Me.GetValue(IsBookmarkedProperty)
    End Get
    Set(ByVal value As Boolean)
        Me.SetValue(IsBookmarkedProperty, value)
    End Set
End Property

Public Shared ReadOnly IsBookmarkedProperty As DependencyProperty = DependencyProperty.Register("IsBookmarked", GetType(Boolean), GetType(Bookmark), New PropertyMetadata(New PropertyChangedCallback(AddressOf OnIsBookmarkedPropertyChanged)))

Private Shared Sub OnIsBookmarkedPropertyChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
    Dim cntrl As Bookmark = TryCast(d, Bookmark)
    cntrl.SetIsBookmarked(e.NewValue)
End Sub

如果您只需要存储该值以供以后使用,那么您不需要在 OnIsBookmarkedPropertyChanged 过程中执行任何操作,但我在那里放置了一些代码作为无论如何,例子。

祝你好运!

Make your IsBookmarked property on the Bookmark control a dependency property.

I presume Control_Loaded is a part of your FormControl, in which case I'm not sure you are using DataContext properly. Best double check that.

UPDATE: Yes, you are using the DataContext properly. AG_E_PARSER_BAD_PROPERTY_VALUE indicates you need to make the IsBookmarked property a dependency property, like so:

 Public Property IsBookmarked() As Boolean
    Get
        Return Me.GetValue(IsBookmarkedProperty)
    End Get
    Set(ByVal value As Boolean)
        Me.SetValue(IsBookmarkedProperty, value)
    End Set
End Property

Public Shared ReadOnly IsBookmarkedProperty As DependencyProperty = DependencyProperty.Register("IsBookmarked", GetType(Boolean), GetType(Bookmark), New PropertyMetadata(New PropertyChangedCallback(AddressOf OnIsBookmarkedPropertyChanged)))

Private Shared Sub OnIsBookmarkedPropertyChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
    Dim cntrl As Bookmark = TryCast(d, Bookmark)
    cntrl.SetIsBookmarked(e.NewValue)
End Sub

If you only need to store the value for later use, then you don't need to do anything in the OnIsBookmarkedPropertyChanged procedure, But I put some code there as an example anyway.

Good Luck!

一直在等你来 2024-09-02 08:17:03

我不记得评估数据绑定的确切顺序(而且我懒得去查找),但据我记得,它最初发生在表单的 Loaded 事件触发之前,并且没有使 IsBookmarked 属性成为依赖项属性,或者至少使用 INotifyPropertyChanged,它可能无法正确建立数据上下文。我建议要么实现 INotifyPropertyChanged 要么将 IsBookmarked 设置为依赖属性。数据绑定在最好的情况下都足够困难(请参阅我对它的长篇大论、脾气暴躁的咆哮此处),如果您没有按照预期的方式设置属性,那么您只会让自己变得更加困难。

I don't recall the exact order in which databinding is evaluated (and I'm too lazy to go look it up), but as I recall, it initially happens BEFORE the form's Loaded event fires, and without making the IsBookmarked property a dependency property, or at least using INotifyPropertyChanged, it may have trouble establishing the datacontext appropriately. I'd recommend either implementing INotifyPropertyChanged or making IsBookmarked a dependency property. DataBinding is tough enough to get right in the best of circumstances (see my long, bad-tempered rant about it here), and you'll just be making it more difficult on yourself if you aren't setting up your properties in the way that it expects.

在梵高的星空下 2024-09-02 08:17:03

该控件公开了一个 IsSiteBookmarked 属性(我认为应该是一个 DependencyProperty),但该控件绑定到一个未显示的 IsBookmarked 属性。这是故意的吗?您是否检查过 Visual Studio 输出窗口是否存在绑定错误?

补充1:
由于您已经修复了问题中的拼写错误并添加了报告的错误。

首先解决 AG_E_PARSER_BAD_PROPERTY_VALUE 问题。错误消息中是否有行号和起始位置?开始寻找那里。一种策略是开始删除 XAML,直到不再出现错误。这将缩小违规代码的范围。

在调试模式下运行,在输出窗口中检查绑定错误。

您可能还想发布 Employee 类代码,尤其是 IsSiteBookmarked 属性。

The control exposes a IsSiteBookmarked property(which I believe should be a DependencyProperty) but the control is binding to a IsBookmarked which is not shown. Is this intentional? Have you checked your Visual Studio output window for binding errors?

Addition 1:
Since you have fixed the typo in your question and added that there is an error being reported.

Start by clearing up the AG_E_PARSER_BAD_PROPERTY_VALUE problem. Is there a line number and start position in the error message? Start looking there. One strategy is to start taking out XAML until there is no longer an error. This will narrow down the offending code.

Running in debug, mode check for binding errors in the output window.

You might want to also post the Employee class code, especially the IsSiteBookmarked property.

甜柠檬 2024-09-02 08:17:03

通常,在对对象进行数据绑定时,您需要利用 INotifyPropertyChanged 接口并实现该接口,以便控件可以正确地使其属性值无效。除非您将 INotifyPropertyChanged 与 Mode=TwoWay 一起使用,否则任何更改 DataContext 的 IsSiteBookmarked 的代码都将无效。

Typically when doing databinding to an object you will want to leverage the INotifyPropertyChanged interface and implement that so that the control can properly invalidate it's property value. Unless you use INotifyPropertyChanged with Mode=TwoWay then any code that changes your DataContext's IsSiteBookmarked will have no effect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文