繁体   English   中英

如何使文本框的ControlTemplate中的按钮更新文本框的文本?

[英]How to make a button in a Textbox's ControlTemplate update the text of the TextBox?

我目前有一个自定义控件,如下所示。 (我正在尝试进行自定义的numericupdown)

基本上,此样式的作用是将两个按钮覆盖在样式文本框的顶部。 这两个按钮应该每次将TextBox的值增加/减少一。 样式很好,并且控件可以正确显示,但是我不知道如何使它起作用。

我需要的

我需要它,因此按钮每次都会将文本框的整数减少/增加一。

我尝试过的

仅供参考,我当前的XAML ResourceDictionary称为Generic.xaml

尝试1

我尝试创建一个名为Generic.xaml.cs的新C#类,并添加x:class="MyProject.Themes.Generic"并为该按钮创建单击事件。 然后,我将适当的事件处理程序添加到Generic.xaml.cs文件中。 我确实设法使click事件起作用,但是我找不到降低TextBox值的方法。

例如:

<Button Click="increaseValue"></Button>
public partial class Generic
{
    private void increaseValue(object sender, RoutedEventArgs e)
    {
    // code to change the textbox's value would be here, but I didn't know how to do it
    }
}

试试2

我尝试了与上述完全相同的方法,但有一些不同。 我将所有样式(在最底部的代码中)嵌套到此:

<Style>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <TextBox>
                    <!-- Styles go here -->
                </TextBox>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

以上都不起作用。 有人有什么想法吗?

下面是我的完整XAML代码(可以正常工作)。 我主要需要背后代码的帮助。

<Style TargetType="{x:Type local1:roundNumericUpDown}">
        <Setter Property="FontFamily" Value="{StaticResource SourceSansRegular}" />
        <Setter Property="Padding" Value="10, 0, 5, 1" />
        <Setter Property="FontSize" Value="15" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="Background" Value="{StaticResource initialColor}" />
        <Setter Property="Height" Value="28px" />
        <Setter Property="HorizontalContentAlignment" Value="Left" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Text" Value="0" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border Background="{TemplateBinding Background}" x:Name="Bd" BorderThickness="0" CornerRadius="15" BorderBrush="#FF383838">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="32px" />
                            </Grid.ColumnDefinitions>
                            <ScrollViewer x:Name="PART_ContentHost" />
                            <Grid Grid.Column="1">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="13" />
                                    <RowDefinition Height="2" />
                                    <RowDefinition Height="13" />
                                </Grid.RowDefinitions>
                                <Button Grid.Row="0" Height="13" VerticalAlignment="Top"  x:Name="IncreaseButton">
                                    <Polygon Points="0,5 16,5 8,0" Stroke="{StaticResource decalColor}" Fill="{StaticResource decalColor}" />
                                    <Button.Style>
                                        <Style TargetType="{x:Type Button}">
                                            <Setter Property="Background" Value="{StaticResource hoverColor}" />
                                            <Setter Property="WindowChrome.IsHitTestVisibleInChrome" Value="True" />
                                            <Setter Property="Template">
                                                <Setter.Value>
                                                    <ControlTemplate TargetType="{x:Type Button}">
                                                        <Border CornerRadius="0, 13, 0 0" x:Name="Bd" Background="{TemplateBinding Background}">
                                                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                                                        </Border>
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                            <Style.Triggers>
                                                <Trigger Property="IsMouseOver" Value="True">
                                                    <Setter Property="Background" Value="{StaticResource clickColor}" />
                                                </Trigger>
                                                <Trigger Property="IsPressed" Value="True">
                                                    <Setter Property="Background" Value="{StaticResource clickColor1}" />
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Button.Style>
                                </Button>
                                <Button Grid.Row="2" Height="13" VerticalAlignment="Bottom" x:Name="DecreaseButton">
                                    <Polygon Points="0,0, 16,0 8,5" Stroke="{StaticResource decalColor}" Fill="{StaticResource decalColor}" />
                                    <Button.Style>
                                        <Style TargetType="{x:Type Button}">
                                            <Setter Property="Background" Value="{StaticResource hoverColor}" />
                                            <Setter Property="WindowChrome.IsHitTestVisibleInChrome" Value="True" />
                                            <Setter Property="Template">
                                                <Setter.Value>
                                                    <ControlTemplate TargetType="{x:Type Button}">
                                                        <Border CornerRadius="0, 0, 13 0" x:Name="Bd" Background="{TemplateBinding Background}">
                                                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                                                        </Border>
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                            <Style.Triggers>
                                                <Trigger Property="IsMouseOver" Value="True">
                                                    <Setter Property="Background" Value="{StaticResource clickColor1}" />
                                                </Trigger>
                                                <Trigger Property="IsPressed" Value="True">
                                                    <Setter Property="Background" Value="{StaticResource clickColor}" />
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Button.Style>
                                </Button>
                            </Grid>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

尝试1是正确的轨道

当然,如果用户将文本更改为非数字值或将TextBox切换为TextBlock,则仅当单击按钮时才更改值,因此我建议为Convert方法添加try catch。号码,您将不需要尝试并抓住。

public partial class Generic
{
    private void increaseValue(object sender, RoutedEventArgs e)
    {
        TextBox textBox = (((((sender as Button).Parent as Grid).Parent as Grid)).Parent as Border).TemplatedParent as TextBox;

        textBox.Text = Convert.ToString(Convert.ToInt32(textBox.Text) + 1);
    }
}

不建议

老实说,我不建议您创建控件的方式。 为您的textBox和按钮创建实际的UserControl并向每个元素(textBox,UpButton,DownButton)添加样式会更加容易和简单。 然后,您将可以自由访问所有3个元素。 通常样式是用于样式:)

希望这能回答您的问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM