我有15个字段几乎完全相同:
private void TextboxMessage1_TextChanged(object sender, TextChangedEventArgs e)
{
SaveMessage(TextboxMessage1.Text, TextboxMessageSeconds1.Text.ToInteger(), 1);
}
private void TextboxMessage2_TextChanged(object sender, TextChangedEventArgs e)
{
SaveMessage(TextboxMessage2.Text, TextboxMessageSeconds2.Text.ToInteger(), 2);
}
private void TextboxMessage3_TextChanged(object sender, TextChangedEventArgs e)
{
SaveMessage(TextboxMessage3.Text, TextboxMessageSeconds3.Text.ToInteger(), 3);
}
etc
调用SaveMessage:
private void SaveMessage(string textboxMessageText, int seconds, int messagenumber)
{
var msg = _configman.MyConfig.MessageConfigs.FirstOrDefault(x => x.MessageNumber == messagenumber);
if (msg == null)
{
var msgconfig = new MessageConfig();
msgconfig.Seconds = seconds;
msgconfig.Command = textboxMessageText;
}
else
{
msg.MessageNumber = messagenumber;
msg.Command = textboxMessageText;
msg.Seconds = seconds;
}
_configman.SaveConfig();
}
我的表格:
<CheckBox x:Name="CheckBoxMessage1" Content="" HorizontalAlignment="Left" Margin="29,23,0,0" VerticalAlignment="Top" Width="15"/>
<TextBox x:Name="TextboxMessage1" Height="23" Margin="49,19,158,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" TextChanged="TextboxMessage1_TextChanged"/>
<Button x:Name="ButtonMessage1" Content="Disabled" HorizontalAlignment="Left" Margin="689,20,0,0" VerticalAlignment="Top" Width="75" Background="#FFFB8686" Click="ButtonMessage1_Click"/>
<CheckBox x:Name="CheckBoxMessage2" Content="" HorizontalAlignment="Left" Margin="29,48,0,0" VerticalAlignment="Top" Width="15"/>
<TextBox x:Name="TextboxMessage2" Height="23" Margin="49,44,158,0" TextWrapping="Wrap" VerticalAlignment="Top" TextChanged="TextboxMessage2_TextChanged"/>
<Button x:Name="ButtonMessage2" Content="Disabled" HorizontalAlignment="Left" Margin="689,45,0,0" VerticalAlignment="Top" Width="75" Background="#FFFB8686"/>
<CheckBox x:Name="CheckBoxMessage3" Content="" HorizontalAlignment="Left" Margin="29,72,0,0" VerticalAlignment="Top" Width="15"/>
<TextBox x:Name="TextboxMessage3" Height="23" Margin="49,68,158,0" TextWrapping="Wrap" VerticalAlignment="Top" TextChanged="TextboxMessage3_TextChanged"/>
<Button x:Name="ButtonMessage3" Content="Disabled" HorizontalAlignment="Left" Margin="689,69,0,0" VerticalAlignment="Top" Width="75" Background="#FFFB8686"/>
有没有办法让TextChanged覆盖我的所有文本框,所以我不必为每个文本框创建一个TextChanged?
它们编号为1-15,例如TextboxMessage1,TextboxMessage1等。
这可能吗?如果是这样,我该怎么做?
答案 0 :(得分:1)
最好的方法是创建一个具有int
属性的新类,该属性继承自TextBox
//Come up with a better name
public class CustomTextBox : TextBox
{
public int Number { get; set; }
}
然后在你的xaml中将属性类型更改为CustomTextBox
(或任何你的名字)。确保添加Number
属性并将TextChanged
属性更改为“TextBoxSave_TextChanged”:
<CustomTextBox x:Name="TextboxMessage1" Height="23" Margin="49,19,158,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" TextChanged="TextBoxSave_TextChanged" Number="1"/>
然后在您的代码中添加一个新方法TextBoxSave_TextChanged()
//You can name it whatever, I would keep the "_TextChanged" suffix though
private void TextBoxSave_TextChanged(object sender, TextChangedEventArgs e)
{
//Cast sender to your new CustomTextBox type
CustomTextBox txtBox = ((CustomTextBox)sender);
SaveMessage(txtBox.Text, txtBox.Text.ToInteger(), txtBox.Number);
}
Ed的建议:
public class CustomTextBox : TextBox
{
#region Number Property
public int Number
{
get { return (int)GetValue(NumberProperty); }
set { SetValue(NumberProperty, value); }
}
public static readonly DependencyProperty NumberProperty =
DependencyProperty.Register(nameof(Number), typeof(int), typeof(CustomTextBox),
new PropertyMetadata(0));
#endregion Number Property
#region Seconds Property
public int Seconds
{
get { return (int)GetValue(SecondsProperty); }
set { SetValue(SecondsProperty, value); }
}
public static readonly DependencyProperty SecondsProperty =
DependencyProperty.Register(nameof(Seconds), typeof(int), typeof(CustomTextBox),
new PropertyMetadata(0));
#endregion Seconds Property
}
TextChanged处理程序:
// You can name it whatever, I would keep the "_TextChanged" suffix though
private void TextBoxSave_TextChanged(object sender, TextChangedEventArgs e)
{
//Cast sender to your new CustomTextBox type
CustomTextBox txtBox = (CustomTextBox)sender;
SaveMessage(txtBox.Text, txtBox.Seconds, txtBox.Number);
}
XAML用法:
<local:CustomTextBox
Number="1"
Seconds="{Binding Text, ElementName=TextboxMessageSeconds1}"
TextChanged="TextBoxSave_TextChanged"
x:Name="TextboxMessage1"
Height="23"
Margin="49,19,158,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
/>
但实际上,这是编写适当的UserControl的不良替代品。