C# - 在循环中启用多个文本框
C# - Enabling multiple textboxes in a loop
我正在编写一个 WPF 程序,它需要有人输入一个数字,然后启用相应数量的文本框。目前我有一个系统可以检查每个文本框,例如
private void navnumbox_TextChanged(object sender, TextChangedEventArgs e)
{
try
{
if (Int32.Parse(navnumbox.Text) > 0)
{
One.IsEnabled = true;
}
if (Int32.Parse(navnumbox.Text) > 1)
{
Two.IsEnabled = true;
}
}
}
我想把它放到一个循环中,我想我可能需要将一和二存储在一个数组中,但我试过了,但我不知道如何访问它们
也许你可以试试这样的东西(抱歉我没能测试)。
private void navnumbox_TextChanged(object sender, TextChangedEventArgs e)
{
var textBoxesMap = new Dictionary<int,TextBox>()
{
{1, One},
{2, Two}
// etc
};
try
{
int number = int.Parse(navnumbox.Text)
foreach(var item in textBoxesMap)
{
if(item.Key <= number)
{
item.Value.IsEnabled = true;
}
}
}
}
而且您显然可以将地图放在其他地方。另一种(更好的)方法是使用 XAML - 关于这个问题还有另一个答案演示了如何这样做。
我认为更好的方法是使用 Converter for Visibility 属性 通过 ElementBinding 将 TextBoxes 1 和 2 转换为 navnumbox Path=Text。
根据您的情况,您需要单独的转换器 TextBox.Enable 用于文本框一和文本框二。
此处XAML为Window:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfApplication2="clr-namespace:WpfApplication2"
Title="MainWindow"
Height="350"
Width="525">
<Window.Resources>
<wpfApplication2:MyConverter x:Key="MyConverter"></wpfApplication2:MyConverter>
</Window.Resources>
<Grid>
<StackPanel>
<TextBox x:Name="MyText"></TextBox>
<TextBox IsEnabled="{Binding ElementName=MyText, Path=Text, Converter={StaticResource MyConverter}}"></TextBox>
</StackPanel>
</Grid>
</Window>
这里是 TexBox 转换器的实现 "One"
public class MyConverter
: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string myValue = value as string;
int result = 0;
if (myValue != null)
{
int.TryParse(myValue,out result);
}
return result > 0;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
// not relevant for your application
throw new NotImplementedException();
}
}
我正在编写一个 WPF 程序,它需要有人输入一个数字,然后启用相应数量的文本框。目前我有一个系统可以检查每个文本框,例如
private void navnumbox_TextChanged(object sender, TextChangedEventArgs e)
{
try
{
if (Int32.Parse(navnumbox.Text) > 0)
{
One.IsEnabled = true;
}
if (Int32.Parse(navnumbox.Text) > 1)
{
Two.IsEnabled = true;
}
}
}
我想把它放到一个循环中,我想我可能需要将一和二存储在一个数组中,但我试过了,但我不知道如何访问它们
也许你可以试试这样的东西(抱歉我没能测试)。
private void navnumbox_TextChanged(object sender, TextChangedEventArgs e)
{
var textBoxesMap = new Dictionary<int,TextBox>()
{
{1, One},
{2, Two}
// etc
};
try
{
int number = int.Parse(navnumbox.Text)
foreach(var item in textBoxesMap)
{
if(item.Key <= number)
{
item.Value.IsEnabled = true;
}
}
}
}
而且您显然可以将地图放在其他地方。另一种(更好的)方法是使用 XAML - 关于这个问题还有另一个答案演示了如何这样做。
我认为更好的方法是使用 Converter for Visibility 属性 通过 ElementBinding 将 TextBoxes 1 和 2 转换为 navnumbox Path=Text。
根据您的情况,您需要单独的转换器 TextBox.Enable 用于文本框一和文本框二。
此处XAML为Window:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfApplication2="clr-namespace:WpfApplication2"
Title="MainWindow"
Height="350"
Width="525">
<Window.Resources>
<wpfApplication2:MyConverter x:Key="MyConverter"></wpfApplication2:MyConverter>
</Window.Resources>
<Grid>
<StackPanel>
<TextBox x:Name="MyText"></TextBox>
<TextBox IsEnabled="{Binding ElementName=MyText, Path=Text, Converter={StaticResource MyConverter}}"></TextBox>
</StackPanel>
</Grid>
</Window>
这里是 TexBox 转换器的实现 "One"
public class MyConverter
: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string myValue = value as string;
int result = 0;
if (myValue != null)
{
int.TryParse(myValue,out result);
}
return result > 0;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
// not relevant for your application
throw new NotImplementedException();
}
}