简体   繁体   English

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

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

I currently have a custom control set up as shown below. 我目前有一个自定义控件,如下所示。 (I am trying to make a custom numericupdown) (我正在尝试进行自定义的numericupdown)

Basically, what this style does is it overlays two buttons on top of a styled text box. 基本上,此样式的作用是将两个按钮覆盖在样式文本框的顶部。 The two buttons should increase/decrease the value in the TextBox by one every time. 这两个按钮应该每次将TextBox的值增加/减少一。 The styling is fine, and the control displays correctly, but I do not know how to make it function. 样式很好,并且控件可以正确显示,但是我不知道如何使它起作用。

WHAT I NEED 我需要的

I need it so the buttons will decrease/increase the textbox's integer by one every time. 我需要它,因此按钮每次都会将文本框的整数减少/增加一。

WHAT I HAVE TRIED 我尝试过的

Just for your information, my current XAML ResourceDictionary is called Generic.xaml . 仅供参考,我当前的XAML ResourceDictionary称为Generic.xaml

Try 1 尝试1

I have tried creating a new C# class called Generic.xaml.cs and adding x:class="MyProject.Themes.Generic" and creating click events for the button. 我尝试创建一个名为Generic.xaml.cs的新C#类,并添加x:class="MyProject.Themes.Generic"并为该按钮创建单击事件。 I would then add the appropriate event handlers into the Generic.xaml.cs file. 然后,我将适当的事件处理程序添加到Generic.xaml.cs文件中。 I did manage to get the click events working, but I couldn't find a way to decrease the value of the TextBox. 我确实设法使click事件起作用,但是我找不到降低TextBox值的方法。

For example: 例如:

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

Try 2 试试2

I have tried the exact same method as above, but with some differences. 我尝试了与上述完全相同的方法,但有一些不同。 I nested all of my styles (in the code at the very bottom) into this: 我将所有样式(在最底部的代码中)嵌套到此:

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

None of the above worked. 以上都不起作用。 Does anyone have any ideas? 有人有什么想法吗?

Below is my full XAML code (it works fine). 下面是我的完整XAML代码(可以正常工作)。 I mainly need assistance with the code-behind. 我主要需要背后代码的帮助。

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

Try 1 is the right track 尝试1是正确的轨道

Of course I recommend to add try catch for the Convert method in-case user change the text to non-numeric value or switch TextBox to TextBlock making the value change only when the buttons are clicked and in that case you can guarantee it to be always number and you will not need the try and catch. 当然,如果用户将文本更改为非数字值或将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);
    }
}

Not recommended 不建议

I honestly do not recommend the way you are creating your control. 老实说,我不建议您创建控件的方式。 It would have been much easier and simpler to create an actual UserControl for your textBox and buttons and and add style to each element (textBox, UpButton, DownButton). 为您的textBox和按钮创建实际的UserControl并向每个元素(textBox,UpButton,DownButton)添加样式会更加容易和简单。 Then you will have the freedom to access all 3 element freely. 然后,您将可以自由访问所有3个元素。 usually Style is for styling :) 通常样式是用于样式:)

Hopefully this answers your question 希望这能回答您的问题

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

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