Windows Universal App中的标签文本框

编程入门 行业动态 更新时间:2024-10-27 22:27:53
本文介绍了Windows Universal App中的标签文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 C#和 XAML 在Windows通用应用程序中实现带有标签的 TextBox ,但是我没有得到 TextBox 的值,只是一个空字符串。

I'm trying to realize a labeled TextBox in a windows universal app using C# and XAML, but I did not get the value of the TextBox in return, just empty string.

我的 generic.xaml 的代码如下:

<Style TargetType="template:LabeledTextBox"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="template:LabeledTextBox"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <TextBlock Text="{TemplateBinding Label}" FontWeight="Bold" VerticalAlignment="Center" Margin="10,0" /> <TextBox Text="{TemplateBinding Value}" IsReadOnly="{TemplateBinding IsReadOnly}" VerticalAlignment="Center" Margin="20,0,10,0" Grid.Row="1" /> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>

控件如下所示:

public sealed class LabeledTextBox : Control, IParameterReturnable { public static readonly DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(LabeledTextBox), new PropertyMetadata(default(string))); public string Label { get { return this.GetValue(LabelProperty).ToString(); } set { this.SetValue(LabelProperty, value); } } public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(LabeledTextBox), new PropertyMetadata(default(string))); public string Value { get { return this.GetValue(ValueProperty).ToString(); } set { this.SetValue(ValueProperty, value); } } public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(LabeledTextBox), new PropertyMetadata(false)); public string IsReadOnly { get { return this.GetValue(IsReadOnlyProperty).ToString(); } set { this.SetValue(IsReadOnlyProperty, value); } } public LabeledTextBox() { this.DefaultStyleKey = typeof(LabeledTextBox); } public LabeledTextBox(Parameter parameter) { this.Label = parameter.DisplayName; this.Value = parameter.DefaultValue ?? ""; this.DefaultStyleKey = typeof(LabeledTextBox); } public string GetKey() { return this.Label; } public string GetValue() { return this.Value; } }

您可以随意忽略我的 IParameterReturnable ,我可以在循环访问不同类型的时使用它访问 GetKey()和 GetValue()这些标有标签的控件。

You are free to ignore my IParameterReturnable, which I am using to access to GetKey() and GetValue() while looping through different types of these labeled-controls.

那么我想念的是什么,我做错了什么?

So what am I missing, what am I doing wrong?

非常感谢

编辑:添加控件的代码

使用C#动态添加控件,如下所示:

The control is added dynamically with C# like this:

foreach (Parameter parameter in parameters) { stackPanel.Children.Add(new LabeledTextBox(parameter)); }

但是在Xaml中看起来像这样:

But it would looks like this in Xaml:

<templates:LabeledTextBox Label="[Label]" Value="[Value]" IsReadOnly="False" />

它将被这样读出:

foreach(IParameterReturnable parameter in stackPanel.Children) { string val = parameter.GetValue() // <= this will always return the default value }

推荐答案

您没有更新 Value 属性,并使用模板化的 TextBox 中的新值。

You are not updating the Value property with a new value from your templated TextBox.

在您的 generic.xaml 中为您的 TextBox 添加一个名称:< TextBox x:Name = PART_TextBox ...

In your generic.xaml add a name to your TextBox: <TextBox x:Name="PART_TextBox"...

然后您的 LabeledTextBox 类应如下所示:

Then your LabeledTextBox class should look like this:

[TemplatePart(Name = "PART_TextBox", Type = typeof(TextBox))] public sealed class LabeledTextBox : Control, IParameterReturnable { public static readonly DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(LabeledTextBox), new PropertyMetadata(default(string))); public string Label { get { return this.GetValue(LabelProperty).ToString(); } set { this.SetValue(LabelProperty, value); } } public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(LabeledTextBox), new PropertyMetadata(default(string))); public string Value { get { return this.GetValue(ValueProperty).ToString(); } set { this.SetValue(ValueProperty, value); } } public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(LabeledTextBox), new PropertyMetadata(false)); public string IsReadOnly { get { return this.GetValue(IsReadOnlyProperty).ToString(); } set { this.SetValue(IsReadOnlyProperty, value); } } public LabeledTextBox() { this.DefaultStyleKey = typeof(LabeledTextBox); } private TextBox _textBox; protected override void OnApplyTemplate() { base.OnApplyTemplate(); _textBox = GetTemplateChild("PART_TextBox") as TextBox; if (_textBox != null) { _textBox.TextChanged += TextBoxOnTextChanged; } } private void TextBoxOnTextChanged(object sender, TextChangedEventArgs textChangedEventArgs) { this.Value = _textBox.Text; } public LabeledTextBox(Parameter parameter) { this.Label = parameter.DisplayName; this.Value = parameter.DefaultValue ?? ""; this.DefaultStyleKey = typeof(LabeledTextBox); } public string GetKey() { return this.Label; } public string GetValue() { return this.Value; } }

请注意,我已经添加了 TemplatePart 属性并覆盖 OnApplyTemplate ,我在其中注册了 TextChanged 事件 TextBox 。当文本更改时,我将更新 Value 依赖项属性。

Notice that I have added the TemplatePart attribute and overriden the OnApplyTemplate in which I register for the TextChanged event of the TextBox. When the text changes, I update the Value dependency property.

更多推荐

Windows Universal App中的标签文本框

本文发布于:2023-11-06 04:17:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562696.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本框   标签   Windows   Universal   App

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!