如何在单击命令上设置文本框值
How to set Textbox value on click command
在我的Resources.xaml(资源词典)上我有一个数据模板:
<DataTemplate x:Key="ProductDetailsContentTemplate">
<StackPanel Orientation="Vertical" >
<Viewbox Margin="27,0,28,0" Height="200" >
<Image UWPUtil:ImageExtensions.CachedUriSource="{Binding ImageUri}" />
</Viewbox>
<TextBlock Text="Description" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" FontSize="24"/>
<TextBlock Text="{Binding ProductDescription}" Style="{StaticResource SubtitleTextBlockStyle}" FontSize="30"/>
<TextBlock Text="Barcode" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" FontSize="24"/>
<TextBlock Text="{Binding Barcode}" Style="{StaticResource SubtitleTextBlockStyle}" FontSize="30"/>
<TextBlock Text="Weight" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" FontSize="24"/>
<TextBlock Text="{Binding Weight}" Style="{StaticResource SubtitleTextBlockStyle}" FontSize="30"/>
<TextBlock Text="Quantity" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" FontSize="24"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25*"/>
<ColumnDefinition Width="50*"/>
<ColumnDefinition Width="25*"/>
</Grid.ColumnDefinitions>
<Button x:Name="SubtractQytyButton" Grid.Column="0" Content="-" FontSize="24" HorizontalAlignment="Stretch"
Margin="5,0" Style="{StaticResource TestButtonStyle}" FontWeight="ExtraBold"
Click="SubQytyButton_Click"/>
<TextBox x:Name="QuantityTextBox" Grid.Column="1" HorizontalAlignment="Stretch" FontSize="24" Margin="5,0"
KeyDown="Quantity_Keydown" TextChanging="Quantity_TextChanging"
Text="{Binding Quantity, Mode=TwoWay, Converter={StaticResource DecmialConverter}}" MaxLength="5" TextAlignment="Center"/>
<Button x:Name="AddQytyButton" Grid.Column="2" Content="+" FontSize="24" HorizontalAlignment="Stretch"
Margin="5,0" Style="{StaticResource TestButtonStyle}" FontWeight="ExtraBold"
Click="AddQytyButton_Click"/>
</Grid>
</StackPanel>
</DataTemplate><br>
我使用 Mads Kristensen 的 File Nesting 文件嵌套它,这是线程:https://marketplace.visualstudio.com/items?itemName=MadsKristensen.FileNesting
所以我的 ResourceDictionary 上有一个 Resources.xaml.cs
我的问题是我想在 AddQytyButton 和 SubQytyButton 单击命令上设置我的 QuantityTextBox 值,这是我的单击命令事件:
private void AddQytyButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
//does not exist in the current context
QuantityTextBox.Text = "test";
}
或
private void AddQytyButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
Button btn = sender as Button;
// this is not working as null reference
var textbox = (btn.Parent as StackPanel).Children[1] as TextBox;
}
谢谢,
尼科廷
您应该将 btn.Parent
转换为 Grid
而不是 StackPanel
,因为您在 XAML.
中使用前者
var textbox = (btn.Parent as Grid).Children[1] as TextBox;
在我的Resources.xaml(资源词典)上我有一个数据模板:
<DataTemplate x:Key="ProductDetailsContentTemplate">
<StackPanel Orientation="Vertical" >
<Viewbox Margin="27,0,28,0" Height="200" >
<Image UWPUtil:ImageExtensions.CachedUriSource="{Binding ImageUri}" />
</Viewbox>
<TextBlock Text="Description" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" FontSize="24"/>
<TextBlock Text="{Binding ProductDescription}" Style="{StaticResource SubtitleTextBlockStyle}" FontSize="30"/>
<TextBlock Text="Barcode" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" FontSize="24"/>
<TextBlock Text="{Binding Barcode}" Style="{StaticResource SubtitleTextBlockStyle}" FontSize="30"/>
<TextBlock Text="Weight" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" FontSize="24"/>
<TextBlock Text="{Binding Weight}" Style="{StaticResource SubtitleTextBlockStyle}" FontSize="30"/>
<TextBlock Text="Quantity" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" FontSize="24"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25*"/>
<ColumnDefinition Width="50*"/>
<ColumnDefinition Width="25*"/>
</Grid.ColumnDefinitions>
<Button x:Name="SubtractQytyButton" Grid.Column="0" Content="-" FontSize="24" HorizontalAlignment="Stretch"
Margin="5,0" Style="{StaticResource TestButtonStyle}" FontWeight="ExtraBold"
Click="SubQytyButton_Click"/>
<TextBox x:Name="QuantityTextBox" Grid.Column="1" HorizontalAlignment="Stretch" FontSize="24" Margin="5,0"
KeyDown="Quantity_Keydown" TextChanging="Quantity_TextChanging"
Text="{Binding Quantity, Mode=TwoWay, Converter={StaticResource DecmialConverter}}" MaxLength="5" TextAlignment="Center"/>
<Button x:Name="AddQytyButton" Grid.Column="2" Content="+" FontSize="24" HorizontalAlignment="Stretch"
Margin="5,0" Style="{StaticResource TestButtonStyle}" FontWeight="ExtraBold"
Click="AddQytyButton_Click"/>
</Grid>
</StackPanel>
</DataTemplate><br>
我使用 Mads Kristensen 的 File Nesting 文件嵌套它,这是线程:https://marketplace.visualstudio.com/items?itemName=MadsKristensen.FileNesting
所以我的 ResourceDictionary 上有一个 Resources.xaml.cs 我的问题是我想在 AddQytyButton 和 SubQytyButton 单击命令上设置我的 QuantityTextBox 值,这是我的单击命令事件:
private void AddQytyButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
//does not exist in the current context
QuantityTextBox.Text = "test";
}
或
private void AddQytyButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
Button btn = sender as Button;
// this is not working as null reference
var textbox = (btn.Parent as StackPanel).Children[1] as TextBox;
}
谢谢,
尼科廷
您应该将 btn.Parent
转换为 Grid
而不是 StackPanel
,因为您在 XAML.
var textbox = (btn.Parent as Grid).Children[1] as TextBox;