自定义 WPF 控件以将 int 属性添加到 TextBox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13825937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom WPF Control to add an int property to TextBox
提问by Ben Walker
I would like an additional WPF control that would add an intproperty to the TextBox class. I have tried Project > Add New Item > Custom Control (WPF). That gave me a new cs file for the new Control. I tried just having this new class inherit TextBoxclass and then adding public int number { get; set; }inside static CustomTextBox()but apparently that is not the correct syntax.
我想要一个额外的 WPF 控件,它将int向 TextBox 类添加一个属性。我试过 Project > Add New Item > Custom Control (WPF)。这给了我一个新控件的新 cs 文件。我试着让这个新类继承TextBox类,然后public int number { get; set; }在里面添加,static CustomTextBox()但显然这不是正确的语法。
The TextBoxs I need this for are to be created dynamically in code, not in XAML.
TextBox我需要它的s 是在代码中动态创建的,而不是在 XAML 中。
Here is my attempt at implementing John Gardner's answer:
这是我尝试实施John Gardner的答案:
public static readonly DependencyProperty Number = DependencyProperty.RegisterAttached(
"number",
typeof(TextBox),
typeof(int),
new PropertyMetadata(false)
);
public static void SetNumber(UIElement element, TextBox value)
{
element.SetValue(Number, value);
}
public static TextBox GetNumber(UIElement element)
{
return (TextBox)element.GetValue(Number);
}
I added this in the MainWindow Class. It does not appear to give my TextBoxs the additional Number property.
我在 MainWindow 类中添加了它。它似乎没有给 my TextBoxs 额外的 Number 属性。
回答by John Gardner
Do you needa new control? You might be better off using an attached property instead. then no new control at all.
你需要一个新的控件吗?您最好改用附加属性。然后根本没有新的控制。
http://msdn.microsoft.com/en-us/library/cc265152(v=VS.95).aspx
http://msdn.microsoft.com/en-us/library/cc265152(v=VS.95).aspx
Update:an attached property doesn't add a property to the textbox directly, you'd access it like
更新:附加属性不会直接将属性添加到文本框,您可以像这样访问它
YourClass.SetNumber( textbox, value );
int value = YourClass.GetNumber( textbox );
or in xaml,
或在 xaml 中,
<TextBox YourClass.Number="1"/>
your property should be "Number" in its string definition as well, you have "number". And your Get/Set calls should have an int value, not a textbox.
您的属性在其字符串定义中也应该是“数字”,您有“数字”。并且您的 Get/Set 调用应该有一个 int 值,而不是一个文本框。
回答by Peter Hansen
You could just create a subclass of TextBox and add a single int property to it. That should do it I guess.
您可以创建 TextBox 的子类并向其添加单个 int 属性。我猜应该可以。
Take a look at this code to see an example of how to do it:
看看这个代码,看看如何做的一个例子:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
panel.Children.Add(new MyTextBox { Number = 123 });
panel.Children.Add(new MyTextBox { Number = 321 });
panel.Children.Add(new MyTextBox { Number = 456 });
panel.Children.Add(new MyTextBox { Number = 654 });
}
private void click(object sender, RoutedEventArgs e)
{
var myTextBoxes = panel.Children.OfType<MyTextBox>();
var numbers = string.Empty;
myTextBoxes.ToList().ForEach(p => numbers += p.Number + Environment.NewLine);
MessageBox.Show(numbers);
}
}
//Subclass of TextBox that just adds one property
public class MyTextBox : TextBox
{
public int Number { get; set; }
}
..and the XAML just has the panel and a button:
..和 XAML 只有面板和一个按钮:
<StackPanel Name="panel">
<Button Content="Show numbers" Click="click" />
</StackPanel>

