C# 在Windows通用应用程序中标记文本框
我试图在windows通用应用程序中使用C#和XAML实现一个带标签的C# 在Windows通用应用程序中标记文本框,c#,wpf,xaml,custom-controls,win-universal-app,C#,Wpf,Xaml,Custom Controls,Win Universal App,我试图在windows通用应用程序中使用C#和XAML实现一个带标签的TextBox,但我没有得到TextBox的值作为回报,只有空字符串 我的generic.xaml的代码如下所示: <Style TargetType="template:LabeledTextBox"> <Setter Property="Template"> <Setter.Value> <ControlTemplate Target
TextBox
,但我没有得到TextBox
的值作为回报,只有空字符串
我的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;
}
}
foreach (Parameter parameter in parameters)
{
stackPanel.Children.Add(new LabeledTextBox(parameter));
}
foreach(IParameterReturnable parameter in stackPanel.Children)
{
string val = parameter.GetValue() // <= this will always return the default value
}
[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;
}
}
您可以随意忽略我的iPareMeterReturnable,我正在使用它访问GetKey()
和GetValue()
,同时循环使用这些带标签的控件的不同类型
那么我错过了什么,我做错了什么
非常感谢您提供的所有有用且善意的答案
编辑:用于添加控件的代码
控件是用C#动态添加的,如下所示:
<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;
}
}
foreach (Parameter parameter in parameters)
{
stackPanel.Children.Add(new LabeledTextBox(parameter));
}
foreach(IParameterReturnable parameter in stackPanel.Children)
{
string val = parameter.GetValue() // <= this will always return the default value
}
[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;
}
}
但在Xaml中看起来是这样的:
<templates:LabeledTextBox Label="[Label]" Value="[Value]" IsReadOnly="False" />
它将这样读出来:
<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;
}
}
foreach (Parameter parameter in parameters)
{
stackPanel.Children.Add(new LabeledTextBox(parameter));
}
foreach(IParameterReturnable parameter in stackPanel.Children)
{
string val = parameter.GetValue() // <= this will always return the default value
}
[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;
}
}
foreach(stackPanel.Children中的iParmeTerreTurnable参数)
{
string val=parameter.GetValue()/您没有使用模板化的文本框中的新值更新值属性
在您的generic.xaml
文本框中添加一个名称
:您可以在放置LabeledTextBox的位置共享xaml吗?对于我循环浏览这些控件的特殊情况,控件是通过c#而不是通过xaml动态添加到stackpanel的。那么如何设置标签和文本属性?您可以将代码添加到列表中吗然后,输入用代码更新。如果您不使用标记
属性,您可以使用它来显示标签,而无需扩展文本框
控件。也许我可以在这里自由地问您:当我得到一个LabeledBoxBox并想要绑定SelectionChanged事件时,静态依赖项应该是什么样子?我添加了我的问题是一个新的话题: