在 MainWindow 中重用 WPF 用户控件
Reusing WPF UserControls in MainWindow
作为 WPF 的初学者,我试图在 WPF 的 MainWindow 视图组件中多次重用具有不同属性的特定用户控件
UserControl FileSelect 包含一个简单的布局,其中包含一个按钮,该按钮包含带有文本框字段的图像。在我的主窗体中,我打算多次使用这个用户控件。即使用不同的图像。
为了从 MainWindow.xaml 设置 Image,我在 UserControl 代码中创建了一个 DependencyProperty,它允许我设置图像文件 属性。
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();
}
}
既然图像 属性 已公开,我认为可以通过 MainWindow.xaml
进行设置
<StackPanel Orientation="Vertical" Grid.Column="0" Grid.ColumnSpan="2">
<View:FileSelectionView FileSelectImage="image01.png"/>
<View:FileSelectionView FileSelectImage="image02.png"/>
.. so on
</StackPanel>
我卡在了这个状态。如何使 属性(用户控件)对 MainWindow.xaml 可用?
此 属性 是只读依赖项 属性。为此 属性 需要 CLR 包装器,即
public string FileSelectImage
{
get { return (string)GetValue(FileSelectImageProperty); }
set { SetValue(FileSelectImageProperty, value); }
}
作为 WPF 的初学者,我试图在 WPF 的 MainWindow 视图组件中多次重用具有不同属性的特定用户控件
UserControl FileSelect 包含一个简单的布局,其中包含一个按钮,该按钮包含带有文本框字段的图像。在我的主窗体中,我打算多次使用这个用户控件。即使用不同的图像。
为了从 MainWindow.xaml 设置 Image,我在 UserControl 代码中创建了一个 DependencyProperty,它允许我设置图像文件 属性。
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();
}
}
既然图像 属性 已公开,我认为可以通过 MainWindow.xaml
进行设置<StackPanel Orientation="Vertical" Grid.Column="0" Grid.ColumnSpan="2">
<View:FileSelectionView FileSelectImage="image01.png"/>
<View:FileSelectionView FileSelectImage="image02.png"/>
.. so on
</StackPanel>
我卡在了这个状态。如何使 属性(用户控件)对 MainWindow.xaml 可用?
此 属性 是只读依赖项 属性。为此 属性 需要 CLR 包装器,即
public string FileSelectImage
{
get { return (string)GetValue(FileSelectImageProperty); }
set { SetValue(FileSelectImageProperty, value); }
}