如何为RadioButton组注册活动?

时间:2016-04-12 14:36:37

标签: c# wpf

我想就以下问题提供一些帮助/解释/澄清:

我有一个嵌套在TreeView中的Expander项,基于不同分支选项的主机RadioButton控件,需要更改相同的属性。单选按钮全部分配给同一组。

   <Expander x:Name="expanderInterestStructure" Header="Interest Structure" HorizontalAlignment="Left" Margin="10,138,0,0" VerticalAlignment="Top"  Width="132" BorderThickness="0">
    <Grid Margin="0,0,10,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <TreeView x:Name="inputInterestStructureSelection" Margin="0,0,-131,-177" Grid.RowSpan="2">
            <TreeViewItem Grid.Row="0" Grid.Column="0" Header="360">
                <RadioButton x:Name="_360fixed" GroupName="interestStructure" Tag="360fixed" Content="Fixed Interest" IsChecked="True"/>
                <TreeViewItem Header="Compund Interest">
                    <RadioButton x:Name="_360compDay" GroupName="interestStructure" Tag="360compDay" Content="Daily"/>
                    <RadioButton x:Name="_360compMonth" GroupName="interestStructure" Tag="360compMonth" Content="Monthly"/>
                    <RadioButton x:Name="_360compQuater" GroupName="interestStructure" Tag="360compQuater" Content="Quaterly"/>
                    <RadioButton x:Name="_360compYear" GroupName="interestStructure" Tag="360compYear" Content="Yearly"/>
                </TreeViewItem>
            </TreeViewItem>
            <TreeViewItem Grid.Row="1" Grid.Column="0"  Header="365/366">
                <RadioButton  x:Name="_365fixed" GroupName="interestStructure" Tag="365fixed" Content="Fixed Interest"/>
                <TreeViewItem Header="Compund Interest">
                    <RadioButton x:Name="_365compDay" GroupName="interestStructure" Tag="365compDay" Content="Daily"/>
                    <RadioButton x:Name="_365compMonth" GroupName="interestStructure" Tag="365compMonth" Content="Monthly"/>
                    <RadioButton x:Name="_365compQuater" GroupName="interestStructure" Tag="365compQuater" Content="Quaterly"/>
                    <RadioButton x:Name="_365compYear" GroupName="interestStructure" Tag="365compYear" Content="Yearly"/>
                </TreeViewItem>
            </TreeViewItem>
        </TreeView>

    </Grid>
</Expander>

问题是,如何注册监控此的事件,如果组成员之间的选择发生更改,它会将字符串属性设置为所选项目标记或名称?

提前致谢!

1 个答案:

答案 0 :(得分:1)

没有组 s ,所有GroupName="interestStructure"都在同一组(RadioButton)。

无论如何,您可以将每个<RadioButton ... Checked="radioButton_Checked"/> 订阅到同一个事件处理程序:

void radioButton_Checked(object sender, RoutedEventArgs e)
{
    var radioButton = (RadioButton)sender; // checked RadioButton

    // logic
    if(radioButton == _360CompDay)
        button.Tag = "whatever";
    else if ...
}

你可以在其中放入所有逻辑:

{{1}}
相关问题