【发布时间】:2015-06-01 00:03:46
【问题描述】:
是否可以从文本框的 textChanged 方法调用单选按钮检查方法?我有一组单选按钮,当一个被选中时,它会调用 radiobutton_Checked 方法。这会进行计算并将结果保存到名为 angle_Offset 的变量中。
我有一个文本框,其文本数据绑定到名为 Heading 的变量。这用于在 radiobutton_Checked 方法中完成的计算。
现在,只要选择单选按钮,计算就会完成。如果我更改文本框中的文本,我必须单击另一个单选按钮以使其更新 angle_Offset 计算,但我正在尝试这样做,以便在文本更改时完成计算而无需选择新的单选按钮(使用当前的)。
这是 xaml:
<TextBox x:Name="Rotate" Text="{Binding ElementName=this, Path=Heading}" TextChanged="textChangedEventHandler" />
<RadioButton Content="0°" Tag="0" GroupName="display" Checked="radiobutton_Checked"/>
<RadioButton Content="90°" Tag="1" GroupName="display" Checked="radiobutton_Checked"/>
<RadioButton Content="180°" Tag="2" GroupName="display" Checked="radiobutton_Checked"/>
<RadioButton Content="270°" Tag="3" GroupName="display" Checked="radiobutton_Checked"/>
下面是代码:
private int Heading
{
get { return (int)GetValue(update_Heading); }
set { SetValue(update_Heading, value); }
}
public static readonly DependencyProperty update_Heading = DependencyProperty.Register("Heading", typeof(int), typeof(MainWindow), new PropertyMetadata(null));
public void radiobutton_Checked(object sender, RoutedEventArgs e)
{
var rb = (RadioButton)sender;
var tag = Convert.ToInt32(rb.Tag);
angle_Offset = (degrees_90 * tag) - Heading;
}
private void textChangedEventHandler(object sender, TextChangedEventArgs args)
{
//not sure how to call radiobutton_Checked from here
}
我试图找出一种从 textChangedEventHandler 调用 radiobutton_Checked 方法的方法,以便更新使用“Heading”变量计算的 angle_Offest(在 radio checked 方法中找到)。我不能只调用它,因为它们有不同的参数。
【问题讨论】:
-
您是否尝试过仅从文本更改函数调用该函数? radiobutton_Checked((object)this.myradiobutton, e);
-
请提供a good, minimal, complete code example,清楚地说明您的问题。我发现对处理单选按钮有用的一个习惯用法是使用
IValueConverter将组状态映射到某个属性值,但是从您的帖子中并不清楚您是如何使用单选组或您想要什么textChanged()方法来做。我的猜测是,您的问题可以在不处理TextChanged事件或Checked事件的情况下解决,但如果没有好的代码示例,则无法提供具体建议。 -
我按照要求提供了完整的代码。
-
我支持马特约翰逊。您可以直接调用 radioButton_checked 方法,即使它采用与 textChanged 方法不同的参数。将参数设置为 radioButton_checked 方法时,只需确保发送者是您需要的 radioButton,并且似乎事件 args 可以保留为 null,因为它没有被使用。
标签: c# wpf radio-button checked textchanged