Collection item data binding in the SilverLight custom collection Control

Source: Internet
Author: User
Tags silverlight

The data verification control encountered a problem today. The property of the Set item in the Custom set control cannot be bound to data. First, list the code of the key part:

Verification control class definition:

Public class Validator: Control {public static readonly DependencyProperty ItemsProperty = DependencyProperty. register ("Items", typeof (ValidatorItemCollection), typeof (Validator), new PropertyMetadata (null); public ValidatorItemCollection Items {get {return (ValidatorItemCollection) base. getValue (ItemsProperty);} set {base. setValue (ItemsProperty, value );}}...... A lot of words are omitted here}
Verification rule set class definition:
public class ValidatorItemCollection : ObservableCollection<ValidatorItem>{}
Verification rule class definition:
public class ValidatorItem : FrameworkElement{         public bool IsRequired { get; set; }    public static readonly DependencyProperty ValidateSenderProperty = DependencyProperty.Register(        "ValidateSender",        typeof(string),        typeof(ValidatorItem),        new PropertyMetadata(string.Empty));    public string ValidateSender    {        get { return (string)GetValue(ValidatorItem.ValidateSenderProperty); }        set        {            SetValue(ValidateSenderProperty, value);        }    }}

XAML code:

<local:Validator x:Name="validator" AutoValidate="{Binding Path=IsEnabled}">    <local:Validator.Items>        <local:ValidatorItem x:Name="item1" IsRequired="True" 
           ValidateSender="{Binding Path=Name, ElementName=textBox1}"></local:ValidatorItem>    </local:Validator.Items></local:Validator>
 
In the above XAML, The ValidateSender attribute of the instantiated ValidatorItem cannot be bound successfully, but if the Binding is successful outside of <local: Validator. Search everywhere
For Binding and ObservableCollection data, the inherited base classes are also replaced. The definition of the DomainDataSource class is also found through decompilation, which still cannot solve the problem. Later
Bind it directly to the C # code and accidentally find that item1 is null. It is expected that a problem occurs in the visual tree: The ValidatorItem defined is not loaded into the control tree at all, although Validator. Items is used
Yes.
 
As a result, I went around to find information about the FindName method and learned that the RegisterName method is required in WPF to register nametypets. However, there is no similar method in silverlight.
 
Finally, I found a solution in this post: http://stackoverflow.com/questions/4571577/silverlight-usercontrol-child-controls-and-findname
 
Here is the answer:

It may be difficult to do at this point but the best course of action wowould probably be to derive your control from ItemsControl instead of UserControl. Then you wouldn't have the problem with name scopes.

I suppose as a workaround you cocould do a dive into the control with VisualTreeHelper to manually set the tbTest field

Later, I changed the Validator class to inherit from ItemsControl, and then killed the Items attribute and ItemsProperty. Finally, the Binding was successful. However, this method does not seem too good. What if I need to define several set attributes in the Validator class?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.