none
textbox内部增加了一个按钮,如何才能做到点击按钮按规则改变textbox的值,不写后台代码 RRS feed

  • 问题

  • textbox内部增加了一个按钮,如何才能做到点击按钮按规则改变textbox的值,不写后台代码

    就像textbox控件自带的X按钮一样。

    我改了textbox的样式,里面增加了两个按钮,效果图如下,如何点击-号就让textbox的值减少,不写后台代码,在metro里面能做到么?

    好像wpf可以

    2012年8月27日 7:21

答案

  • 这个怎么可能不写代码,你这样需要做一个自定义控件了,肯定少不了控件本身的逻辑的,比如下面这个依据你需求做的Templated Control:

        public sealed class ValueTextBox : Control
        {
            public ValueTextBox()
            {
                this.DefaultStyleKey = typeof(ValueTextBox);
            }
            Button AddButton;
            Button SubButton;
            TextBox ValueBox;
            protected override void OnApplyTemplate()
            {
                base.OnApplyTemplate();
                ValueBox = this.GetTemplateChild("valueBox") as TextBox;
                AddButton = this.GetTemplateChild("addButton") as Button;
                SubButton = this.GetTemplateChild("subButton") as Button;
                if (AddButton != null)
                {
                    AddButton.Click -= AddButton_Click;
                    AddButton.Click += AddButton_Click;
                }
                if (SubButton != null)
                {
                    SubButton.Click -= SubButton_Click;
                    SubButton.Click += SubButton_Click;
                }
            }
            void SubButton_Click(object sender, RoutedEventArgs e)
            {
                double result = 0;
                if (ValueBox != null)
                {
                    double.TryParse(ValueBox.Text, out result);
                    ValueBox.Text = (result - 0.01).ToString();
                }
            }
            void AddButton_Click(object sender, RoutedEventArgs e)
            {
                double result=0;
                if (ValueBox != null)
                {
                    double.TryParse(ValueBox.Text, out result);
                    ValueBox.Text = (result + 0.01).ToString();
                }
            }
        }

    Theme XAML:

        <Style TargetType="local:ValueTextBox">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="local:ValueTextBox">
                        <Border
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                                <TextBox x:Name="valueBox" Grid.Column="1"/>
                                <Button x:Name="addButton" Content="+" Foreground="White" Grid.Column="2" Width="50" Height="40"/>
                                <Button x:Name="subButton" Content="-" Foreground="White" Grid.Column="0" Width="50" Height="40"/>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    Sample download: https://skydrive.live.com/#cid=51B2FDD068799D15&id=51B2FDD068799D15%211090

    P.S. WPF也要实现如自定义控件的方式,也要写代码的除非你直接用现成的别人写好的。


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    • 已标记为答案 kande_1988 2012年8月29日 2:57
    2012年8月28日 6:53
    版主

全部回复

  • 这个怎么可能不写代码,你这样需要做一个自定义控件了,肯定少不了控件本身的逻辑的,比如下面这个依据你需求做的Templated Control:

        public sealed class ValueTextBox : Control
        {
            public ValueTextBox()
            {
                this.DefaultStyleKey = typeof(ValueTextBox);
            }
            Button AddButton;
            Button SubButton;
            TextBox ValueBox;
            protected override void OnApplyTemplate()
            {
                base.OnApplyTemplate();
                ValueBox = this.GetTemplateChild("valueBox") as TextBox;
                AddButton = this.GetTemplateChild("addButton") as Button;
                SubButton = this.GetTemplateChild("subButton") as Button;
                if (AddButton != null)
                {
                    AddButton.Click -= AddButton_Click;
                    AddButton.Click += AddButton_Click;
                }
                if (SubButton != null)
                {
                    SubButton.Click -= SubButton_Click;
                    SubButton.Click += SubButton_Click;
                }
            }
            void SubButton_Click(object sender, RoutedEventArgs e)
            {
                double result = 0;
                if (ValueBox != null)
                {
                    double.TryParse(ValueBox.Text, out result);
                    ValueBox.Text = (result - 0.01).ToString();
                }
            }
            void AddButton_Click(object sender, RoutedEventArgs e)
            {
                double result=0;
                if (ValueBox != null)
                {
                    double.TryParse(ValueBox.Text, out result);
                    ValueBox.Text = (result + 0.01).ToString();
                }
            }
        }

    Theme XAML:

        <Style TargetType="local:ValueTextBox">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="local:ValueTextBox">
                        <Border
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                                <TextBox x:Name="valueBox" Grid.Column="1"/>
                                <Button x:Name="addButton" Content="+" Foreground="White" Grid.Column="2" Width="50" Height="40"/>
                                <Button x:Name="subButton" Content="-" Foreground="White" Grid.Column="0" Width="50" Height="40"/>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    Sample download: https://skydrive.live.com/#cid=51B2FDD068799D15&id=51B2FDD068799D15%211090

    P.S. WPF也要实现如自定义控件的方式,也要写代码的除非你直接用现成的别人写好的。


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    • 已标记为答案 kande_1988 2012年8月29日 2:57
    2012年8月28日 6:53
    版主
  • 好吧。。。我想多了。。谢谢。
    2012年8月29日 2:57