Windows Universal App中的标签文本框 [英] Labeled TextBox in Windows Universal App

查看:81
本文介绍了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中的标签文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆