【问题标题】:How to set Textbox value on click command如何在点击命令上设置文本框值
【发布时间】:2017-06-12 00:55:45
【问题描述】:

在我的 Resources.xaml(Resource Dictionary) 上,我有一个 DataTemplate:

<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;
    }

谢谢,
尼古丁

【问题讨论】:

    标签: c# uwp resourcedictionary


    【解决方案1】:

    您应该将 btn.Parent 转换为 Grid 而不是 StackPanel,因为您在 XAML 中使用的是前者。

    var textbox = (btn.Parent as Grid).Children[1] as TextBox;
    

    【讨论】:

    • 我可以使用 var textbox = (btn.Parent as StackPanel).Children[1] as TextBox;在那?请提供代码谢谢
    • 尝试使用 MVVM 并设置它。
    • 我不认为这是一个好方法,如果你改变你无法得到描述的代码顺序
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    • 1970-01-01
    • 2016-07-23
    • 2013-07-07
    • 2017-07-27
    相关资源
    最近更新 更多