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?