c# - 如何使文本框的 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>
解决方案
尝试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 个元素。通常样式用于样式:)
希望这能回答你的问题
推荐阅读
- flutter - Flutter 通过 Firestore 参考
- liferay-7 - 我们如何在 portletmvc4spring 中添加多个控制器并显示 JSP
- c++ - pybind11:python 列表到 std::vector 的自动转换
? - selenium - 在 WrapsElement 接口中声明的 getWrappedElement() 方法有什么用,为什么只有 Select 类实现它?
- angular - Angular http帖子编号数组
- java - 将字符串拆分为列表,排序并保持顺序
- json - 在 Reactjs 中,如何从我已经放置图像链接的 json 文件中返回图像?
- python - 在 Excel 中双击一个单元格
- python - 如何摆脱 VSCode 中的特定 tabnine 推荐
- panel-data - 如何使用 pgmm 添加虚拟交互