我有几个文本框,其默认值为10,每个框旁边有两个按钮,这些按钮用于将文本框值增加1或单击减少一个。我知道我可以简单地为每个按钮创建一个单击事件处理程序,但我想这样做,而不是该方法可以通过从xaml传递它来找到指定的文本框。原谅我,如果这显然是怎么做的,但我很难过。
我已经进入绑定但无法弄清楚如何利用它来使其工作。
<Grid Background="DarkMagenta" Margin="0,0,0,-31">
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="0,10, 0, 20">
<Label>Set Your Stats!</Label>
<StackPanel Orientation="Vertical" HorizontalAlignment="Left">
<Label>Strenght</Label>
<StackPanel Orientation="Horizontal">
<TextBox Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button Click="Button_Click">+</Button>
<Button>-</Button>
</StackPanel>
</StackPanel>
<Label>Dexterity</Label>
<StackPanel Orientation="Horizontal">
<TextBox Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button>+</Button>
<Button>-</Button>
</StackPanel>
</StackPanel>
<Label>Constitution</Label>
<StackPanel Orientation="Horizontal">
<TextBox Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button>+</Button>
<Button>-</Button>
</StackPanel>
</StackPanel>
<Label>Intelligence</Label>
<StackPanel Orientation="Horizontal">
<TextBox Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button>+</Button>
<Button>-</Button>
</StackPanel>
</StackPanel>
<Label>Wisdom</Label>
<StackPanel Orientation="Horizontal">
<TextBox Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button>+</Button>
<Button>-</Button>
</StackPanel>
</StackPanel>
<Label>Charisma</Label>
<StackPanel Orientation="Horizontal">
<TextBox Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button>+</Button>
<Button>-</Button>
</StackPanel>
</StackPanel>
</StackPanel>
<Button HorizontalAlignment="Stretch" Margin="80,0,80,0" >Continue</Button>
</StackPanel>
CS。文件方法
public partial class StatSelection : Page
{
CharacterDetails charDetails = new CharacterDetails();
public StatSelection(CharacterDetails addedDetails)
{
charDetails = addedDetails;
InitializeComponent();
}
private void IncrementBy1(object sender, RoutedEventArgs e)
{
// code for incrementing the textbox by 1
}
}
我希望这个方法有一个或两个方法,根据每次调用时传递给方法的文本框,找到正确的文本框。
您可以尝试以下代码,找到与该按钮关联的文本框,并将该值递增1。
private void IncrementBy1(object sender, RoutedEventArgs e)
{
// code for incrementing the textbox by 1
Button btn = sender as Button;
var parent = VisualTreeHelper.GetParent(VisualTreeHelper.GetParent(btn));
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var textBox = VisualTreeHelper.GetChild(parent, i) as TextBox;
if (textBox != null)
{
var value = int.Parse(textBox.Text);
textBox.Text = (++value).ToString();
break;
}
}
}
感谢user2818985和Nawed Nabi Zada的回答。使用您的建议我找到了一种方法,只对所有按钮使用一次点击事件!
<Grid Background="DarkMagenta" Margin="0,0,0,-31">
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="0,10, 0, 20">
<Label>Set Your Stats!</Label>
<StackPanel Orientation="Vertical" HorizontalAlignment="Left">
<Label>Strenght</Label>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="StrTxtBx" Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button x:Name="StrIncrease" Click="IncreaseDecrease">+</Button>
<Button x:Name="StrDecrease" Click="IncreaseDecrease">-</Button>
</StackPanel>
</StackPanel>
<Label>Dexterity</Label>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="DexTxtBx" Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button x:Name="DexIncrease" Click="IncreaseDecrease">+</Button>
<Button x:Name="DexDecrease" Click="IncreaseDecrease">-</Button>
</StackPanel>
</StackPanel>
<Label>Constitution</Label>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="ConTxtBx" Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button x:Name="ConIncrease" Click="IncreaseDecrease">+</Button>
<Button x:Name="ConDecrease" Click="IncreaseDecrease">-</Button>
</StackPanel>
</StackPanel>
<Label>Intelligence</Label>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="IntTxtBx" Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button x:Name="IntIncrease" Click="IncreaseDecrease">+</Button>
<Button x:Name="IntDecrease" Click="IncreaseDecrease">-</Button>
</StackPanel>
</StackPanel>
<Label>Wisdom</Label>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="WisTxtBx" Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button x:Name="WisIncrease" Click="IncreaseDecrease">+</Button>
<Button x:Name="WisDecrease" Click="IncreaseDecrease">-</Button>
</StackPanel>
</StackPanel>
<Label>Charisma</Label>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="ChrTxtBx" Margin="10,10,2,10" MinHeight="10" MaxHeight="20" MinWidth="15" MaxWidth="20">10</TextBox>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button x:Name="ChrIncrease" Click="IncreaseDecrease">+</Button>
<Button x:Name="ChrDecrease" Click="IncreaseDecrease">-</Button>
</StackPanel>
</StackPanel>
</StackPanel>
<Button HorizontalAlignment="Stretch" Margin="80,0,80,0" >Continue</Button>
</StackPanel>
</Grid>
Cs代码
private void IncreaseDecrease(object sender, RoutedEventArgs e)
{
string btnName = ((Button)sender).Name;
int TxtBxVal;
switch(btnName)
{
case "StrIncrease":
TxtBxVal = int.Parse(StrTxtBx.Text);
StrTxtBx.Text = (++TxtBxVal).ToString();
break;
case "StrDecrease":
TxtBxVal = int.Parse(StrTxtBx.Text);
StrTxtBx.Text = (--TxtBxVal).ToString();
break;
case "DexIncrease":
TxtBxVal = int.Parse(DexTxtBx.Text);
DexTxtBx.Text = (++TxtBxVal).ToString();
break;
case "DexDecrease":
TxtBxVal = int.Parse(DexTxtBx.Text);
DexTxtBx.Text = (--TxtBxVal).ToString();
break;
case "ConIncrease":
TxtBxVal = int.Parse(ConTxtBx.Text);
ConTxtBx.Text = (++TxtBxVal).ToString();
break;
case "ConDecrease":
TxtBxVal = int.Parse(ConTxtBx.Text);
ConTxtBx.Text = (--TxtBxVal).ToString();
break;
case "IntIncrease":
TxtBxVal = int.Parse(IntTxtBx.Text);
IntTxtBx.Text = (++TxtBxVal).ToString();
break;
case "IntDecrease":
TxtBxVal = int.Parse(IntTxtBx.Text);
IntTxtBx.Text = (--TxtBxVal).ToString();
break;
case "WisIncrease":
TxtBxVal = int.Parse(WisTxtBx.Text);
WisTxtBx.Text = (++TxtBxVal).ToString();
break;
case "WisDecrease":
TxtBxVal = int.Parse(WisTxtBx.Text);
WisTxtBx.Text = (--TxtBxVal).ToString();
break;
case "ChrIncrease":
TxtBxVal = int.Parse(ChrTxtBx.Text);
ChrTxtBx.Text = (++TxtBxVal).ToString();
break;
case "ChrDecrease":
TxtBxVal = int.Parse(ChrTxtBx.Text);
ChrTxtBx.Text = (--TxtBxVal).ToString();
break;
}
}
有了这个,盒子根据需要增加和减少。 可能会考虑将来削减它的方法,但现在我很高兴。再次感谢你们!