0

I want the combo box to be enable when pressing one of the radio buttons.

<RadioButton x:Name="A" GroupName="rButton" Content="A" Grid.Column="4"/>
<RadioButton x:Name="B" GroupName="rButton" Content="B" Grid.Column="4"/>
<RadioButton x:Name="C" GroupName="rButton" Content="C" Grid.Column="4"/>
<RadioButton x:Name="D" GroupName="rButton" Content="D" Grid.Column="4"/>

<ComboBox IsEnabled="{Binding IsChecked,?? }" Grid.Column="5" Width="120" Height="30"/>
sivan24
  • 43
  • 6

1 Answers1

2

If you want to solve this via Bindings (and you should), you need a MultiBindingConverter that returns true as long as one of the values is true (boolean OR):

public class BooleanOrConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        foreach (object value in values)
        {
            if (value is bool && (bool) value)
                return true;
        }
        return false;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        return Enumerable.Repeat(DependencyProperty.UnsetValue, targetTypes.Length).ToArray();
    }
}

Definition:

<Window.Resources>
    <local:BooleanOrConverter x:Key="OrConverter"/>
</Window.Resources>

Usage:

<RadioButton x:Name="RadioButtonSource" GroupName="rButton" Content="A" Grid.Column="4"/>
<RadioButton x:Name="RadioButtonToken" GroupName="rButton" Content="B" Grid.Column="4"/>
<RadioButton x:Name="RadioButtonII" GroupName="rButton" Content="C" Grid.Column="4"/>
<RadioButton x:Name="RadioButtonUkey" GroupName="rButton" Content="D" Grid.Column="4"/>

<ComboBox Grid.Column="5" Width="120" Height="30">
    <ComboBox.IsEnabled>
        <MultiBinding Converter="{StaticResource OrConverter}">
            <Binding ElementName="RadioButtonSource" Path="IsChecked"/>
            <Binding ElementName="RadioButtonToken" Path="IsChecked"/>
            <Binding ElementName="RadioButtonII" Path="IsChecked"/>
            <Binding ElementName="RadioButtonUkey" Path="IsChecked"/>
        </MultiBinding>
    </ComboBox.IsEnabled>
</ComboBox>

This way, as soon as any of the RadioButtons's IsChecked properties becomes true, the ComboBox is enabled. If you reset the RadioButtons, it get's disabled again.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
  • If it is only a visual operation, why it should be done using viewmodel? In my opinion the UI stuff should be expleted in code behind. There is no reason to pass through viewmodel if not only to complicate a simple task that can be done with a click event – Daniele Sartori Nov 15 '17 at 08:25
  • @DanieleSartori I have no idea what you are on about, there is absolutely no ViewModel in my code. As for *"In my opinion the UI stuff should be expleted in code behind"* I completely disagree with that statement. Would you generate `VisualStates`, `StoryBoards`, `DataTemplates` and `Styles` in code-behind too? I hope not. – Manfred Radlwimmer Nov 15 '17 at 08:28
  • @DanieleSartori I disagree with you. UI stuff should be in XAML or ResourceDictionaries. – Sasha Nov 15 '17 at 08:31
  • My bad, i misread your code. However, i would never dream to do VisualStates, StoryBoards, DataTemplates and Styles in code behind. This should be done in xaml. I'm referring to this kind of operation(i.e. enabling or disabling controls if this is related to another operation on the UI) – Daniele Sartori Nov 15 '17 at 08:31
  • @DanieleSartori Actually, the *best* way to do this would be to use a ViewModel, but there is none in OPs code, so I didn't introduce one. Btw.: (regarding your answer) How would RadioButton_Click ever result in an unchecked RadioButton? I think you can remove the last two lines of your code. – Manfred Radlwimmer Nov 15 '17 at 08:33
  • @DanieleSartori Lets say you have 20 controls like radioButton. If you will write simple tasks in codeBehind, then if you will reuse this control, and lets say you would like to change a little bit behavior on the RadioButtonClick event, then you have some problems. With MVVM you can just use new VM – Sasha Nov 15 '17 at 08:36
  • @Sasha I'm not saying MVVM is wrong. I'm saying that of course everything depends on what you are developing. If you need like the op a simple operation of enabling/disabling something based on a click, it has no sense, in my opinion, to fill the viewmodel with properties and methods that are created with the only purpose to handle a simple UI operation and make it heavier than already it is in a normal application – Daniele Sartori Nov 15 '17 at 08:42
  • @DanieleSartori But that's **exactly** what a ViewModel is for. If you have multiple Views you can use the same ViewModel without writing additional code for each View. Sure, if in a certain special case (Ok Button in a dialog, PasswordBoxes, etc.) It's easier to use code-behind, by all means do it, but for everything else - use a ViewModel. – Manfred Radlwimmer Nov 15 '17 at 08:44
  • @DanieleSartori Your simple contol would grow with a time. So would simple operations (like enable/disable) in code behind. At some point in future, you will see, that this control has a lot of codebehind - so you would not be able to reuse xaml or viewModel. – Sasha Nov 15 '17 at 08:49
  • @Sasha i think that this answer explain pretty much what i mean https://stackoverflow.com/questions/6421372/why-to-avoid-the-codebehind-in-wpf-mvvm-pattern . I think that be a "purist" (i.e. Only initialize component in code behind), in some case can be negative. But again, that is my thought – Daniele Sartori Nov 15 '17 at 09:01
  • @DanieleSartori sure there are some specific cases, when you must use code behind and app would not crash. But still, you should try to avoid code behind as much as possible. – Sasha Nov 15 '17 at 09:13
  • @Sasha we are going strongly off topic here, and i made a mistake starting this discussion here.. Everyone codes following their believs, and i think that view/UI related code should stay in code behind. The viewmodel must present the data to the view and handle all the operation about them, not the operation that concerns UI only. – Daniele Sartori Nov 15 '17 at 09:18
  • @Manfred Radlwimmer Thank you – sivan24 Nov 15 '17 at 09:18
  • @sivan24 You are welcome. If this answers your question, please click the check-mark at the top left of my answer to mark this question as answered. – Manfred Radlwimmer Nov 15 '17 at 09:22