Home > front end >  Disable Combobox in WPF using Powershell
Disable Combobox in WPF using Powershell

Time:02-23

I was trying to disable/Gray out the combo box once I select any other item like Radio Button Item. I have created the WPF form using Visual Studio & used the same xaml form in PowerShell. So, in the backend are the PowerShell functions which executes based on the selection. One such requirement is to get data when I select a Radio Button against a Combo Box. Item selected in the combo box should match with other combo box item otherwise my function wont work. The problem, which I'm facing is I'm not able Disable/Gray out it, even I tried by setting the item isenable=$false in PowerShell. If I set the same in XAML , it works but it doesn't fulfill the requirement otherwise I wont be able to select the items at all. Here is the code

<RadioButton Name="Radio_VM" Content="ASR Status" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="30,382,0,0" RenderTransformOrigin="0.133,0.393"/>
        <ComboBox Name="Client" HorizontalAlignment="Left" Margin="15,230,0,0" VerticalAlignment="Top" Width="120" IsDropDownOpen="False" SelectedIndex="2"  >
            <ComboBoxItem Content="ABC"/>
            <ComboBoxItem Content="DEF"/>
        </ComboBox>

As soon as I select Radiobutton, it should Gray Out Combo Box.

CodePudding user response:

You could use a Style with a DataTrigger that binds to the IsChecked property of the RadioButton:

<ComboBox Name="Client" HorizontalAlignment="Left" Margin="15,230,0,0" VerticalAlignment="Top" Width="120" IsDropDownOpen="False" SelectedIndex="2">
    <ComboBox.Style>
        <Style TargetType="ComboBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsChecked, ElementName=Radio_VM}" Value="True">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ComboBox.Style>
    <ComboBoxItem Content="ABC"/>
    <ComboBoxItem Content="DEF"/>
</ComboBox>
  • Related