代码之家  ›  专栏  ›  技术社区  ›  MattE

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

  •  0
  • MattE  · 技术社区  · 7 年前

    通过简单地绑定控件的IsEnabled属性,我可以让这个功能与文本框和组合框一起工作,我想一直隐藏,直到有东西被选中。比如文本,所以:

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

    因此,基本上,这个项目将不会显示,直到有人在组合框中选择的东西。

    如何使用一组单选按钮获得相同的功能?基本上,我有一个stackpanel,我希望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}">
    

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

    1 回复  |  直到 7 年前
        1
  •  0
  •   zambonee    7 年前

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