【问题标题】:Binding content of one Radio button in group 2 with selected radio button in group 1将组 2 中的一个单选按钮的内容与组 1 中选定的单选按钮绑定
【发布时间】:2014-06-26 12:23:52
【问题描述】:

我的 WPF xaml 中有两组单选按钮。

<StackPanel Margin="5,-4,0,5">
 <RadioButton Content="abc" GroupName="grp1" IsChecked="true" Margin=" 8,-4,8,5"/>
 <RadioButton Content="def" GroupName="grp1" IsChecked="false" Margin=" 8,-4,8,5"/>
</StackPanel>


<StackPanel Margin="5,-4,0,5">
 <RadioButton Content="{Binding ElementName}" GroupName="grp2" IsChecked="true" Margin=" 8,-4,8,5"/>
 <RadioButton Content="xyz" GroupName="grp2" IsChecked="false" Margin=" 8,-4,8,5"/>                         
</StackPanel>

我想将 grp2 单选按钮的内容与 grp1 单选按钮中的 SelectedName 绑定。 例如:如果在 grp1 中选择了 abc 单选按钮,则第一个单选按钮的名称应为 Value_*,其中 * 是 abc 或 def,具体取决于所选单选按钮。

谢谢, 罗希特。

【问题讨论】:

    标签: wpf xaml c#-4.0 data-binding


    【解决方案1】:

    将两者绑定到 ViewModel 中的 DependencyProperty。

    【讨论】:

      【解决方案2】:

      你的意思是这样的吗?

      <StackPanel Margin="5,-4,0,5">
              <RadioButton Content="abc" GroupName="grp1" x:Name="AbcRadioBtn" IsChecked="true" Margin=" 8,-4,8,5"/>
              <RadioButton Content="def" GroupName="grp1" x:Name="DefRadioBtn" IsChecked="false" Margin=" 8,-4,8,5"/>
      </StackPanel>
      <StackPanel Margin="5,-4,0,5">
              <RadioButton GroupName="grp2" IsChecked="true" Margin=" 8,-4,8,5">
                  <RadioButton.Style>
                      <Style TargetType="RadioButton">
                          <Style.Triggers>
                              <DataTrigger Binding="{Binding ElementName=AbcRadioBtn, Path=IsChecked}" Value="True
                                           ">
                                  <Setter Property="Content" Value="{Binding ElementName=AbcRadioBtn,Path=Content}"></Setter>
                              </DataTrigger>
      
                              <DataTrigger Binding="{Binding ElementName=DefRadioBtn, Path=IsChecked}" Value="True">
                                  <Setter Property="Content" Value="{Binding ElementName=DefRadioBtn,Path=Content}"></Setter>
                              </DataTrigger>
                          </Style.Triggers>
                      </Style>
                  </RadioButton.Style>
              </RadioButton>
              <RadioButton Content="xyz" GroupName="grp2" IsChecked="false" Margin=" 8,-4,8,5"/>
      </StackPanel>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-04
        • 2017-07-24
        • 1970-01-01
        • 2021-05-24
        相关资源
        最近更新 更多