【问题标题】:WPF Radio Button Group How to enable a TextBox when any item gets selected?WPF单选按钮组如何在选择任何项目时启用文本框?
【发布时间】:2019-04-12 13:16:24
【问题描述】:

我可以通过简单地绑定控件的 IsEnabled 属性来让此功能与文本框和组合框一起使用没有问题,我希望保持隐藏状态,直到将某些内容选择到 .Text 属性,如下所示:

<Label Content="Please Select the status for this client:" Margin="0,0,65,0" IsEnabled="{Binding CBCustomer.Text}" />

所以基本上这个项目不会显示,直到有人在 ComboBox 中选择了一些东西。

如何使用一组单选按钮获得相同的功能?基本上我有一个堆栈面板,我希望将 IsEnabled 属性(显示为 XXXXXX)绑定到是否选择了任何按钮的布尔值。

  <StackPanel Orientation="Horizontal" Canvas.Left="54"  Canvas.Top="40" Margin="0,0,0,50">
            <Label Content="Please Select the RAG Status for this client:" Margin="0,0,65,0" IsEnabled="{Binding CBCustomer.Text}" />
            <RadioButton GroupName="RAGGroup" Margin="0,8,0,0" Background="Red" Foreground="Red" IsEnabled="{Binding CBCustomer.Text}">Red</RadioButton>
            <RadioButton GroupName="RAGGroup" Margin="10,8,0,0" Background="DarkGoldenrod" Foreground="DarkGoldenrod" IsEnabled="{Binding CBCustomer.Text}">Amber</RadioButton>
            <RadioButton GroupName="RAGGroup" Margin="10,8,0,0" Background="Green" Foreground="Green" IsEnabled="{Binding CBCustomer.Text}">Green</RadioButton>
        </StackPanel>

  <StackPanel Margin="55,80,0,0" IsEnabled="{Binding XXXXXXX}">

使用多重绑定?我看到的例子展示了如何从被选中的按钮中取回值,但我并不关心,我只需要看看这个值是否被选中。

【问题讨论】:

    标签: c# wpf data-binding radio-group


    【解决方案1】:

    如果要绑定其他元素,可以使用DataTriggers

    <RadioButton x:Name="RB1"/>
    <RadioButton x:Name="RB2"/>
    <RadioButton x:Name="RB3"/>
    <StackPanel>
        <StackPanel.Style>
            <Style TargetType="StackPanel">
                <Setter Property="IsEnabled" Value="False"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=RB1, Path=IsChecked}" Value="True">
                        <Setter Property="IsEnabled" Value="True"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ElementName=RB2, Path=IsChecked}" Value="True">
                        <Setter Property="IsEnabled" Value="True"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ElementName=RB3, Path=IsChecked}" Value="True">
                        <Setter Property="IsEnabled" Value="True"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </StackPanel.Style>
    </StackPanel>
    

    但我几乎总是绑定到数据层,比如:

    <RadioButton IsChecked="{Binding RAG, Converter=...}"/>
    <RadioButton IsChecked="{Binding RAG, Converter=...}"/>
    <RadioButton IsChecked="{Binding RAG, Converter=...}"/>
    <StackPanel>
        <StackPanel.Style>
            <Style TargetType="StackPanel">
                <Setter Property="IsEnabled" Value="True"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RAG}" Value="{x:Null}">
                        <Setter Property="IsEnabled" Value="False"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </StackPanel.Style>
    </StackPanel>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      • 2013-06-28
      相关资源
      最近更新 更多