首页 > 解决方案 > 如何使文本框的 ControlTemplate 中的按钮更新文本框的文本?

问题描述

我目前有一个自定义控件设置,如下所示。(我正在尝试制作自定义 numericupdown)

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

我需要的

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

我尝试过的

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

试试 1

我尝试创建一个名为的新 C# 类,并为按钮Generic.xaml.cs添加x:class="MyProject.Themes.Generic"和创建单击事件。然后我会将适当的事件处理程序添加到Generic.xaml.cs文件中。我确实设法让点击事件正常工作,但我找不到降低文本框值的方法。

例如:

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

标签: c#wpfxaml

解决方案


尝试1是正确的轨道

当然,我建议为 Convert 方法添加 try catch,以防用户将文本更改为非数字值或将 TextBox 切换为 TextBlock,从而仅在单击按钮时才更改值,在这种情况下,您可以保证它始终编号,您将不需要尝试和捕捉。

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 个元素。通常样式用于样式:)

希望这能回答你的问题


推荐阅读