问题描述
将我的自定义控件拖到设计视图中时,它只是将其放入 xaml.cs 中。
<MyCustomInputBoxView HorizontalAlignment="Left" Margin="167,103,0" VerticalAlignment="Top"/>
但是,当我从工具箱中将文本框拖入设计视图时,它会出现默认变量,例如:
<TextBox HorizontalAlignment="Left" Margin="69,0" Text="TextBox" textwrapping="Wrap" Width="120"/>
已经在xaml中填写了。
如何为我自己的自定义控件设置这些默认值?我到处找,甚至不知道要搜索什么才能找到答案。
我希望我的控件在拖入设计视图时看起来像这样:
<MyCustomInputBoxView TextBoxContent="Text" Label="Text" HorizontalAlignment="Left" Margin="167,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"。我想对我的控件做类似的事情。
解决方法
@wyattk。 当我将控件拖到 XAML 中时,该控件不显示关联的属性。而且我查了相关文档,没有找到相关的设置。这可能是控件的 Visual Studio 设置。 为了设置自定义控件属性的默认值,我做了一个自定义控件,当它从“工具箱”拖到设计视图中时,它有DependencyProperty Angle和一个默认值。这是我的代码,你可以参考它来设置自定义控件的默认值。
xaml 代码:
<Grid>
<local:MyControl Content="MyControl" HorizontalAlignment="Left" Margin="330,221,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 类。我在 Visual Studio Enterprise 2019 Version 16.9.3 中创建了我的程序。 请将 Microsoft.Windows.Design.Extensibility 和 Microsoft.Windows.Design.Interaction 添加到您的项目参考。 步骤是:在你的程序中右击References,在弹出的对话框中选择Add Reference...,然后在Reference Manager页面选择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,typeof(CustomControl1),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");
}
}
}
}
然后将此控件拖放到 Design 上,您将看到以下 XAML 代码:
<local:CustomControl1 x:Name="myControl" Angle="70.0" HorizontalAlignment="Left" Height="100" Margin="252,66,0" VerticalAlignment="Top" Width="100"/>