我有几个文本框,它们的默认值是10,每个文本框旁边有两个按钮,这些文本框应该用于将文本框的值增加1或单击一次减少1。 我确实知道我可以简单地为每个按钮创建一个click事件处理程序,但我想这样做,因此该方法可能会通过从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
}
}
我希望该方法具有一个或两个方法,以根据每次调用该方法时传递给该方法的文本框来找到合适的文本框以递增。
答案 0 :(得分:0)
您可以尝试以下代码,该代码找到与按钮关联的文本框,并将值增加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;
}
}
}
答案 1 :(得分:0)
感谢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;
}
}
此框根据需要增加或减少。
将来可能会考虑减少它的方式,但现在对此我不满意。
再次谢谢你们!