简体   繁体   English

在MainWindow中重用WPF UserControls

[英]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 作为WPF的初学者,我试图使用不同的属性多次重用WPF的MainWindow视图组件中的特定用户控件。

The UserControl FileSelect contains a simple layout which includes a button containing an image with a textbox field. UserControl FileSelect包含一个简单的布局,该布局包含一个按钮,该按钮包含带有文本框字段的图像。 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. 要从MainWindow.xaml设置图像 ,我已经在UserControl代码内部创建了DependencyProperty,这将允许我设置Image File属性。

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 现在,公开了Image属性,我假定可以通过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? 如何使此依赖项属性(用户控件)可用于MainWindow.xaml?

This property is a readonly Dependency property. 此属性是只读的Dependency属性。 You need CLR wrapper here for this property ie 您需要此属性的CLR包装器,即

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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