试图从文本框,单选按钮和滑块控件wpf获取值。

时间:2015-04-14 01:36:48

标签: c# xaml slider radio-button wpf-controls

你好我试图从3种不同的控件类型(文本框,单选按钮和滑块)中获取值来计算贷款的每月付款。我无法获得单选按钮和滑块的值。

如果选中其他按钮,则年份(15,30,其他)有3个单选按钮,然后用户在文本框中输入年份。

滑块收集利率。我不确定如何获得用户滑动的价值。

输出应该在标签中。

    private void RadioButton_Checked(object sender, RoutedEventArgs e)
    {

    }
    void radiobutton_CheckChanged(object sender, RoutedEventArgs e)
    {
        //txtYears.IsEnabled = btn3.IsChecked;
    }

    private void sldRate_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {

    }

    private void btnSubmit_Click(object sender, RoutedEventArgs e)
    {

        double p, yrs, r, m;

        double.TryParse(txtPrin.Text, out p);

        foreach (RadioButton button in stkYears.Children)
        {
            if (button.IsChecked.Value)
            {
                if (button.Content.ToString() == "Other")
                    txtYears.IsEnabled = btn3.IsChecked;           
            }
        }
        double.TryParse(txtYears.Text, out yrs);

        m = (txtPrin *  )
            //the above statment should  look like this 
            //m = (p * r / 1200.0) / (1 - Math.Pow((1.0 + r / 1200.0), (-12 * yrs)));
        lblMonthlyPayment.TextInput = m.ToString();           

    }
下面的

是xaml

<Label Content="Number Of Years" HorizontalAlignment="Left" VerticalAlignment="Top" Width="175" FontFamily="NSimSun" FontSize="14" Height="26" Grid.Row="1" Margin="0,28,0,0"/>
        <StackPanel Grid.Column="1" Name="stkYears" Margin="0,6,0,0" Grid.Row="1">
            <RadioButton x:Name="btn1" GroupName="Years" Content="15 Years" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="99" Height="15" FontFamily="Shruti" Grid.Column="1" Grid.Row="1"/>
            <RadioButton GroupName="Years"  Content="30 Years" HorizontalAlignment="Left" Margin="10,1,0,0" VerticalAlignment="Top" Width="99" Height="15" FontFamily="Shruti" />
            <RadioButton x:Name="btn3" GroupName="Years"  Checked="radioButton_CheckChanged" Unchecked="radioButton_CheckChanged" Content="Other" HorizontalAlignment="Left" Margin="10,1,0,0" 
                           VerticalAlignment="Top" Width="99" Height="15" FontFamily="Shruti" IsChecked="True"/>
            <TextBox x:Name="txtYears" Height="17" Margin="30,0,168,0"/>
        </StackPanel>

        <Label Content="Interest Rate" HorizontalAlignment="Left" Margin="0,27,0,0" VerticalAlignment="Top" Width="188" FontFamily="NSimSun" FontSize="14" Height="24" Grid.Row="2" Grid.ColumnSpan="2"/>
        <Slider HorizontalAlignment="Left" Name="sldRate" IsSnapToTickEnabled="True" TickPlacement="BottomRight" 
             AutoToolTipPlacement="BottomRight" TickFrequency="3"
             AutoToolTipPrecision="2" IsDirectionReversed="False"
             IsMoveToPointEnabled="False" Margin="13,27,-18,0" VerticalAlignment="Top" Width="291" Minimum="0" Maximum="6" Ticks="0, 1, 2, 3, 4, 5, 6" ValueChanged="sldRate_ValueChanged" Height="51" Background="White" Foreground="Black" BorderBrush="#00000000" Grid.Column="1" Grid.Row="2"/>



        <Button Name ="btnSubmit" Content="Calculate Monthly Payment" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="165" Click="btnSubmit_Click" Height="30" Grid.Row="3" RenderTransformOrigin="0.5,0.5"/>
        <Label x:Name="lblMonthlyPayment" Content="" Grid.Column="1" HorizontalAlignment="Left" Margin="13,15,0,0" Grid.Row="3" VerticalAlignment="Top" Width="124" IsEnabled="False" />

2 个答案:

答案 0 :(得分:0)

回答问题1:

在其他单选按钮IsChecked="true"

时启用文本框
<TextBox x:Name="txtYears" Height="17" IsEnabled="{Binding ElementName=btn3,Path=IsChecked}" Margin="30,0,168,0"/>

回答问题2:

在代码中获取滑块值

double value = System.Convert.ToDouble(sldRate.TickFrequency.ToString());

在xaml中,

       <Slider HorizontalAlignment="Left" Name="sldRate" IsSnapToTickEnabled="True" TickPlacement="BottomRight" 
                     AutoToolTipPlacement="BottomRight" TickFrequency="3"
                     AutoToolTipPrecision="2" IsDirectionReversed="False"
                     IsMoveToPointEnabled="False" Margin="23,110,0,0" VerticalAlignment="Top" Width="291" Minimum="0" Maximum="100" 
                      ValueChanged="sldRate_ValueChanged" Height="51" 
Background="White" Foreground="Black" BorderBrush="#00000000"/>

回答问题3:

根据

背后的代码中的单选按钮IsChecked获取值
double years;
            if(btn1.IsChecked == true)
            {
                years = 15;
            }
            else if(btn2.IsChecked == true)
            {
                years = 30;
            }
            else if(btn3.IsChecked == true)
            {
                years = System.Convert.ToDouble(txtYears.Text);
            }

注意:如果您正在认真编写企业级应用程序,请遵循MVVM模式。不要在模式后面使用经典的winform代码。 在WPF中学习MVVM和数据绑定概念。

答案 1 :(得分:0)

要在选中单选按钮时启用文本框,您需要将TextBox的IsEnabled属性绑定到单选按钮的IsChecked属性:

<TextBox x:Name="txtYears" Height="17" IsEnabled="{Binding ElementName=btn3,Path=IsChecked}" Margin="30,0,168,0"/>

要获取滑块的值,您只需要在代码隐藏文件中使用其名称访问其值,如下所示:

var sliderValue = sldRate.Value.ToString()