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

trying to get values from text box, radio button and slider controls wpf.

你好,我正在尝试从 3 种不同的控件类型(文本框、单选按钮和滑块)中获取值,以计算贷款的每月还款额。我无法获取单选按钮和滑块的值。

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

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

输出应该在标签中。

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

问题 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 的答案:

后台代码根据Radio button 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 和数据绑定概念。

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

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

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

var sliderValue = sldRate.Value.ToString()