簡體   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