2

I have BudgetControlType Properties that has 1 .. 7 value

if(BudgetControlType ==1)
 dataComboBox1.Visibility=Visibility.Visiblile;
 dataComboBox2 to dataComboBox7 =Visibility.Hidden;

if(BudgetControlType ==2)
 dataComboBox1.Visibility=Visibility.Visiblile;
 dataComboBox2.Visibility=Visibility.Visiblile;
 dataComboBox3 to dataComboBox7 =Visibility.Hidden;

and so on...

How to do this in xaml?

Elham Azadfar
  • 709
  • 2
  • 17
  • 34

3 Answers3

2

Here is another approach I have used in the past using WPFConverters.

<TabItem.Visibility>
    <Binding Path="SomeObservableCollection.Count">
        <Binding.Converter>
            <converters:ConverterGroup>
                <converters:ExpressionConverter Expression="{}{0} &gt; 0" />
                <BooleanToVisibilityConverter />
            </converters:ConverterGroup>
        </Binding.Converter>
    </Binding>
</TabItem.Visibility>

The ConvertGroup allows for multiple converters to be run sequentially.

The ExpressionConverter lets you define an arbitrary expression. In my case I want the TabItem to be visible if the collection count is greater than zero. Being defined in xaml means escaping characters and a somewhat awkward syntax but it works well enough!

The BooleanToVisibilityConverter converts the boolean result from the expression to our desired visibility.

For Elham, BudgetControlType could be bound to as long as it implemented INotifyPropertyChanged. An equals expression is done like this (I'm returning true if the bound value equals 7):

<converters:ExpressionConverter Expression="{}{0} == 7" />
David Hollinshead
  • 1,710
  • 17
  • 18
2

You can use 1,2,4,8,... and convert it to Visibility

for example if your int number is 6 (2+4) then Control with paramerter 2 and Control with parameter 4 is Visible!

 public class IntToVisibilityConverter:IValueConverter
    {
        private int val;
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int intParam = (int)parameter;
            val = (int)value;

            return ((intParam & val) != 0) ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

And in xaml :

<ComboBox Visibility="{Binding Path=MyEnum,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource IntToVisibilityConverter}, ConverterParameter=1}"/>

<ComboBox Visibility="{Binding Path=MyEnum,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource IntToVisibilityConverter}, ConverterParameter=2}"/>

<ComboBox Visibility="{Binding Path=MyEnum,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource IntToVisibilityConverter}, ConverterParameter=4}"/>
Ali Bayat
  • 3,561
  • 2
  • 42
  • 43
1

The best way I'd say would be to go with properties on your ViewModel, and bind to them.

example (you'll have to massage it a bit, but it's fairly simple from here) :

public Visibility dtcb1 { get; set; }
// all the rest till 7

// Somewhere in your logit / constructor :
dtcb1 = BudgetControlType == 1 ? Visible : Hidden;
// and so on

And on your xaml you'll bind your visibility to dtcb1 You can make the property boolean, and use a boolean to visibility converter as well (as per this answer for example, or just google yourself)

Community
  • 1
  • 1
Noctis
  • 11,507
  • 3
  • 43
  • 82