WPF usercontrol and expose design their own property

Design your own UserControl beneficial reuse controls, for the later work has compounded the general benefits.

Design their own usercontrol just drag the direct combination of simple controls, need to do a complete package, you need to define dependency properties that can be used directly in the xaml when the integrated use of the latter, the example in this article is describes how to define your own dependency properties and how to set dependency property in the callback function. First on the code:

 public List<string> Labels
        {
            get { return (List<string>)GetValue(LabelsProperty); }
            set { SetValue(LabelsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Labels.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LabelsProperty =
            DependencyProperty.Register("Labels", typeof(List<string>), typeof(EEGCurvePresentUserControl), new PropertyMetadata(new PropertyChangedCallback(onLabelsChanged)));
        static void onLabelsChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            List<string> labels = (List<string>)args.NewValue;
            chinnalNum = labels.Count;
            CurvePresentUserControl curvePresentUserControl = (CurvePresentUserControl)sender;
            curvePresentUserControl.LabelsListBox.ItemsSource = labels;
        }

This code is part of the project is required to write a high-speed waveform display module, so they used to achieve a writeablebitmap

Code-dependent attributes most of the code can be directly used propdp tab and double-click to automatically generate, and then use the tab to set the name they need, in a

 new PropertyMetadata(new PropertyChangedCallback(onLabelsChanged))

和 Curve Present User Control curve presented User Control = (Curve Present User Control) transceiver;

A callback function is set to perform at the xaml required when using binding, it is a component parameter passed to obtain access to the relevant controls.

Here if you do not set this callback function but to write the relevant code directly in the set, the property can only be set in the background when the code is executed, and after the xaml binding and can not be executed to change INotifyPropertyChanged.

Published 19 original articles · won praise 2 · Views 5169

Guess you like

Origin blog.csdn.net/gunjiu4462/article/details/90405507