I have control with RadioButtons.
Each RadioButton
represent some state of document. Each document have a lot of labels, buttons and another trash.
I need to collapse different labels, buttons, other...
In different states.
I think that I am building big crutch. What is the better way to implement this functionality?
XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1.Converters"
xmlns:enums="clr-namespace:WpfApplication1.Enums"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:MultiBooleanToVisibilityConverter x:Key="Converter" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Orientation="Vertical">
<RadioButton Content="First" x:Name="First"></RadioButton>
<RadioButton Content="Second" x:Name="Second"></RadioButton>
<RadioButton Content="Third" x:Name="Third"/>
<RadioButton Content="Fourth" x:Name="Fourth"></RadioButton>
</StackPanel>
<StackPanel Orientation="Vertical" Grid.Row="1">
<Label Content="Test1">
<Label.Visibility>
<MultiBinding Converter="{StaticResource Converter}">
<MultiBinding.ConverterParameter>
<x:Array Type="{x:Type sys:Enum}">
<enums:DocumentTypes>Second</enums:DocumentTypes>
<enums:DocumentTypes>Fourth</enums:DocumentTypes>
</x:Array>
</MultiBinding.ConverterParameter>
<Binding ElementName="First" Path="IsChecked" />
<Binding ElementName="Second" Path="IsChecked" />
<Binding ElementName="Third" Path="IsChecked" />
<Binding ElementName="Fourth" Path="IsChecked" />
</MultiBinding>
</Label.Visibility>
</Label>
<Label Content="Test2">
<Label.Visibility>
<MultiBinding Converter="{StaticResource Converter}">
<MultiBinding.ConverterParameter>
<x:Array Type="{x:Type sys:Enum}">
<enums:DocumentTypes>First</enums:DocumentTypes>
<enums:DocumentTypes>Third</enums:DocumentTypes>
</x:Array>
</MultiBinding.ConverterParameter>
<Binding ElementName="First" Path="IsChecked" />
<Binding ElementName="Second" Path="IsChecked" />
<Binding ElementName="Third" Path="IsChecked" />
<Binding ElementName="Fourth" Path="IsChecked" />
</MultiBinding>
</Label.Visibility>
</Label>
<Label Content="Test3">
<Label.Visibility>
<MultiBinding Converter="{StaticResource Converter}">
<MultiBinding.ConverterParameter>
<x:Array Type="{x:Type sys:Enum}">
<enums:DocumentTypes>Fourth</enums:DocumentTypes>
<enums:DocumentTypes>First</enums:DocumentTypes>
</x:Array>
</MultiBinding.ConverterParameter>
<Binding ElementName="First" Path="IsChecked" />
<Binding ElementName="Second" Path="IsChecked" />
<Binding ElementName="Third" Path="IsChecked" />
<Binding ElementName="Fourth" Path="IsChecked" />
</MultiBinding>
</Label.Visibility>
</Label>
</StackPanel>
</Grid>
</Window>
ENUM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApplication1.Enums
{
public enum DocumentTypes
{
First, Second, Third, Fourth
}
}
CONVERTER
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using WpfApplication1.Enums;
namespace WpfApplication1.Converters
{
class MultiBooleanToVisibilityConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Dictionary<string, bool> dict = new Dictionary<string, bool>();
dict.Add("First", (bool)values[0]);
dict.Add("Second", (bool)values[1]);
dict.Add("Third", (bool)values[2]);
dict.Add("Fourth", (bool)values[3]);
if (parameter != null)
{
List<DocumentTypes> result = ((IEnumerable)parameter).Cast<DocumentTypes>().ToList();
foreach (DocumentTypes type in result)
{
if (dict[type.ToString()])
{
return System.Windows.Visibility.Visible;
}
}
}
return System.Windows.Visibility.Collapsed;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}