[英]WPF-VS2019 How do I customize a control to have default values when dragging from the toolbox into design view?
当将我的自定义控件拖到设计视图中时,它只是将它放在 xaml.cs 中的空白处。
<MyCustomInputBoxView HorizontalAlignment="Left" Margin="167,103,0,0" VerticalAlignment="Top"/>
但是,当我从工具箱将文本框拖到设计视图中时,它会在默认变量中扑通一声,例如:
<TextBox HorizontalAlignment="Left" Margin="69,0,0,0" Text="TextBox" TextWrapping="Wrap" Width="120"/>
已在 xaml 中填写。
如何为我自己的自定义控件设置这些默认值? 我到处寻找,甚至不知道要搜索什么来找到答案。
我希望我的控件在拖入设计视图时看起来像这样:
<MyCustomInputBoxView TextBoxContent="Text" Label="Text" HorizontalAlignment="Left" Margin="167,103,0,0" VerticalAlignment="Top"/>
类似于上面的 TextBox 有Text="TextBox"和TextWrapping="Wrap" ,我希望我的自动输入TextBoxContent="Text"和Label="Text"
我查看了 TextBox.cs 的源代码,并没有看到任何明显的点使它自动执行此操作。 这是 TextBox 文本的依赖属性和公共属性。
/// <summary>
/// The DependencyID for the Text property.
/// Default Value: ""
/// </summary>
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register(
"Text", // Property name
typeof(string), // Property type
typeof(TextBox), // Property owner
new FrameworkPropertyMetadata( // Property metadata
string.Empty, // default value
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | // Flags
FrameworkPropertyMetadataOptions.Journal,
new PropertyChangedCallback(OnTextPropertyChanged), // property changed callback
new CoerceValueCallback(CoerceText),
true, // IsAnimationProhibited
UpdateSourceTrigger.LostFocus // DefaultUpdateSourceTrigger
));
和
/// <summary>
/// Contents of the TextBox.
/// </summary>
[DefaultValue("")]
[Localizability(LocalizationCategory.Text)]
public string Text
{
get { return (string) GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
感谢任何阅读本文并可以提供帮助的人。
编辑:带有“SfTextInputLayout”的Syncfusion示例,当我将它拖到设计视图上时,它会自动添加一个文本框子项并用“John”填充该文本框文本。
<Syncfusion:SfTextInputLayout Hint="Name">
<TextBox Text="John"/>
</Syncfusion:SfTextInputLayout>
Hint="name" 和 Text="John" 是在控件拖到设计视图时自动添加的。 我想用我的控件做类似的事情。
@怀亚特克。 当我将控件拖入 XAML 时,控件不显示关联的属性。 而且我查阅了相关文档,但没有找到相关的设置。 这可能是控件的 Visual Studio 设置。 为了设置自定义控件属性的默认值,我做了一个自定义控件,当它从“工具箱”拖到设计视图中时,它有 DependencyProperty Angle 和一个默认值。 这是我的代码,您可以参考它来设置自定义控件的默认值。
xaml 代码:
<Grid>
<local:MyControl Content="MyControl" HorizontalAlignment="Left" Margin="330,221,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
自定义控制代码:
public class MyControl : Button
{
static double num = 90.0;
public MyControl()
{
this.Initialized += (s, e) =>
{
var element = s as UIElement;
if (element != null)
{
element.RenderTransformOrigin = new Point(0.5, 0.5);
element.RenderTransform = new RotateTransform(num);
}
};
}
public double Angle
{
get { return (double)GetValue(AngleProperty); }
set { SetValue(AngleProperty, value); }
}
public static readonly DependencyProperty AngleProperty =
DependencyProperty.RegisterAttached("Angle", typeof(double), typeof(MyControl), new PropertyMetadata(num, OnAngleChanged));
private static void OnAngleChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
num = (double)e.NewValue;
var element = obj as UIElement;
if (element != null)
{
element.RenderTransformOrigin = new Point(0.5, 0.5);
element.RenderTransform = new RotateTransform(num);
}
}
}
更新:
为 VS 设计器提供默认值。 您可以尝试使用DefaultInitializer class。 我在 Visual Studio Enterprise 2019 版本 16.9.3 中创建了我的程序。 请将 Microsoft.Windows.Design.Extensibility 和 Microsoft.Windows.Design.Interaction 添加到您的项目参考中。 步骤是:在你的程序中右键References,在弹出的对话框中右键select Add Reference…,然后在Reference Manager页面中就可以找到select Assemblies,然后搜索依赖
CustomControl1 代码如下:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.Windows.Design.Model;
using Microsoft.Windows.Design.Features;
namespace MyControl
{
[Feature(typeof(myDefaults))]
public class CustomControl1 : Control
{
static CustomControl1()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
}
static double num = 70.0;
public CustomControl1()
{
this.Initialized += (s, e) =>
{
var element = s as UIElement;
if (element != null)
{
element.RenderTransformOrigin = new Point(0.5, 0.5);
element.RenderTransform = new RotateTransform(num);
}
};
}
public double Angle
{
get { return (double)GetValue(AngleProperty); }
set { SetValue(AngleProperty, value); }
}
public static readonly DependencyProperty AngleProperty =
DependencyProperty.RegisterAttached("Angle", typeof(double), typeof(CustomControl1), new PropertyMetadata(num, OnAngleChanged));
private static void OnAngleChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
num = (double)e.NewValue;
var element = obj as UIElement;
if (element != null)
{
element.RenderTransformOrigin = new Point(0.5, 0.5);
element.RenderTransform = new RotateTransform(num);
}
}
public class myDefaults : DefaultInitializer
{
public override void InitializeDefaults(ModelItem item)
{
item.Name = "myControl";
item.Properties["Angle"].SetValue("70.0");
}
}
}
}
然后将此控件拖放到设计上,您将在 XAML 中看到以下代码:
<local:CustomControl1 x:Name="myControl" Angle="70.0" HorizontalAlignment="Left" Height="100" Margin="252,66,0,0" VerticalAlignment="Top" Width="100"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.