Today I am going to share a simple trick to make any object selectable in User interface so that it can be bind to Treeview, checkbox list or radio button list. The trick is simple, we have to warp the user custom class with another class named “SelectableData”.
Bellow the code segment is given
public class SelectableData<T>:ANotifyPropertyChanged
{
public SelectableData()
{
_isEnabled = true;
}
private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
_isSelected = value;
OnPropertyChanged("IsSelected");
}
}
private T _data;
protected bool _isEnabled;
public T Data
{
get { return _data; }
set
{
_data = value;
OnPropertyChanged("Data");
}
}
public bool IsEnabled
{
get { return _isEnabled; }
set
{
_isEnabled = value;
OnPropertyChanged("IsEnabled");
}
}
}
Bellow the uses is given.
private void GenerateSelectableOption(string[] selectedOptions)
{
string[] options = new string[] {"Data", "Data1"};
var selectableOptions = new List<SelectableData<string>>();
foreach (var option in options)
{
selectableOptions.Add(new SelectableData<string>
{
Data = option,
IsSelected = selectedOptions.Any(o => o.Equals(option))
});
}
SelectableOptions = selectableOptions;
}
public List<SelectableData<string>> SelectableOptions
{
get { return (List<SelectableData<string>>)GetValue(SelectableOptionsProperty); }
set { SetValue(SelectableOptionsProperty, value); }
}
public static readonly DependencyProperty SelectableOptionsProperty =
DependencyProperty.Register("SelectableOptions", typeof(List<SelectableData<string>>),
typeof(OptionsEditorPresenter), new UIPropertyMetadata(null));
And the Xaml Binding Code is given bellow
<UserControl x:Class="OptionsEditorView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="300" Height="200">
<UserControl.Resources>
<DataTemplate x:Key="OptionsCheckboxTemplate" >
<CheckBox Content="{Binding Data}" IsChecked="{Binding IsSelected}" Margin="0 0 5 0"/>
</DataTemplate>
<DataTemplate x:Key="OptionsRadioButtonTemplate" >
<RadioButton GroupName="options" Content="{Binding Data}" IsChecked="{Binding IsSelected}" Margin="0 0 5 0"/>
</DataTemplate>
<Style x:Key="ItemControlStyle" TargetType="{x:Type ItemsControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding Multiselect}" Value="False">
<Setter Property="ItemTemplate" Value="{StaticResource OptionsRadioButtonTemplate}"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Multiselect}" Value="True">
<Setter Property="ItemTemplate" Value="{StaticResource OptionsCheckboxTemplate}"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid>
<ItemsControl ItemsSource="{Binding SelectableOptions}" Style="{StaticResource ItemControlStyle}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</UserControl>
Hope this helps, until next time.
No comments:
Post a Comment