繁体   English   中英

WPF单选按钮组当选择任何项目时如何启用文本框?

[英]WPF Radio Button Group How to enable a TextBox when any item gets selected?

通过将我想保持隐藏状态的控件的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}">

使用多重绑定? 我看到了一些示例,这些示例说明了如何从选定的按钮取回值,但是我对此并不在乎,我只需要查看是否已选择该值即可。

如果要绑定到其他元素,可以使用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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM