简体   繁体   中英

Reusing WPF UserControls in MainWindow

Being a beginner in WPF, I'm trying to reuse a particular user control in my MainWindow view component in WPF multiple times with different properties

The UserControl FileSelect contains a simple layout which includes a button containing an image with a textbox field. In my main form, I plan to use this user control multiple times. ie with different images.

fileselector

To set the Image from the MainWindow.xaml I have created a DependencyProperty inside the UserControl code which will allow me to set the Image File property.

public partial class FileSelectionView : UserControl
    {
        public string GetFileSelectImage(DependencyObject obj)
        {
            return (string)obj.GetValue(FileSelectImageProperty);
        }

        public void SetFileSelectImage(DependencyObject obj, string value)
        {
            obj.SetValue(FileSelectImageProperty, value);
        }

        // Using a DependencyProperty as the backing store for FileSelectImage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FileSelectImageProperty =
            DependencyProperty.RegisterAttached("FileSelectImage", typeof(string), typeof(FileSelectionView), new PropertyMetadata("flash.png", OnImageFileChanged));

        private static void OnImageFileChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(d)) return;

            FileSelectionView fv = ((FileSelectionView)(FrameworkElement)d);
            if (fv != null)
            {
                Image tb = (Image)fv.imgButtonFileSelect;

                //Image tb = ((System.Windows.Controls.Image)(FrameworkElement)d);
                //var imageConverter = new ImageSourceConverter();
                if (tb != null)
                {
                    tb.Source = new BitmapImage(new Uri("Images\\" + (string)e.NewValue, UriKind.Relative));
                }
            }
        }

        public FileSelectionView()
        {
            InitializeComponent();
        }
    }

Now that the Image property is exposed I assume that it can be set via MainWindow.xaml

<StackPanel Orientation="Vertical" Grid.Column="0" Grid.ColumnSpan="2">
      <View:FileSelectionView FileSelectImage="image01.png"/>
      <View:FileSelectionView FileSelectImage="image02.png"/>
      .. so on
</StackPanel>

I'm stuck at this state. How do I make this dependency property (usercontrol) available to the MainWindow.xaml?

This property is a readonly Dependency property. You need CLR wrapper here for this property ie

public string FileSelectImage
{
    get { return (string)GetValue(FileSelectImageProperty); }
    set { SetValue(FileSelectImageProperty, value); }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM