This article will show you how you can perform Custom Validations on Radio buttons in MVVM using WPF in c#.net. This will help to select multiple radio button in same form.
I have written four questions on page and associated with radio buttons so user should select the radio button as yes or No then only validation will get remove for that particular question.
I have written four questions on page and associated with radio buttons so user should select the radio button as yes or No then only validation will get remove for that particular question.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WpfApplication1.ViewModel"
Title="MainWindow" Height="350" Width="525" >
<Window.Resources>
<!--<local:ValidationViewModel
x:Key="myvm"/>-->
</Window.Resources>
<Grid HorizontalAlignment="Left" x:Name="mygrid" VerticalAlignment="Center" Width="519" >
<Grid.Resources>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="20" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="1" FontSize="14" Grid.ColumnSpan="2">Question1?</Label>
<Label Grid.Row="1" Grid.Column="1" FontSize="14" Grid.ColumnSpan="2">Question2?</Label>
<Label Grid.Row="2" Grid.Column="1" FontSize="14" Grid.ColumnSpan="2">Question3?</Label>
<Label Grid.Row="3" Grid.Column="1" FontSize="14" Grid.ColumnSpan="2">Question4?</Label>
<Line x:Name="myline" Grid.Row="0" Grid.Column="4" X1="0" Y1="20" Stroke="Red" StrokeThickness="4">
<Line.Style>
<Style TargetType="Line">
<Setter Property="Visibility" Value="Visible"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsValidateQuestion1}" Value="true">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsValidateQuestion1}" Value="false">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Line.Style>
</Line>
<ListBox
x:Name="Question1"
BorderThickness="0"
Grid.Column="4"
Grid.Row="0"
SelectedValuePath="Tag"
Margin="7,4,-7,5">
<ListBox.Resources>
<ResourceDictionary>
<System:Boolean x:Key="YesValue">True</System:Boolean>
<System:Boolean x:Key="NoValue">False</System:Boolean>
</ResourceDictionary>
</ListBox.Resources>
<ListBox.SelectedValue>
<Binding
Mode="TwoWay"
Path="IsValidateQuestion1"
UpdateSourceTrigger="PropertyChanged">
</Binding>
</ListBox.SelectedValue>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<RadioButton
Margin="0,0,10,0"
x:Name="myradio"
Content="{TemplateBinding Content}"
IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBoxItem Tag="{StaticResource YesValue}">Yes</ListBoxItem>
<ListBoxItem Tag="{StaticResource NoValue}">No</ListBoxItem>
</ListBox>
<Line x:Name="myline1" Grid.Row="1" Grid.Column="4" X1="0" Y1="20" Stroke="Red" StrokeThickness="4" >
<Line.Style>
<Style TargetType="Line">
<Setter Property="Visibility" Value="Visible"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsValidateQuestion2}" Value="true">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsValidateQuestion2}" Value="false">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Line.Style>
</Line>
<ListBox
x:Name="Question2"
BorderThickness="0"
Grid.Column="4"
Grid.Row="1"
SelectedValuePath="Tag"
Margin="13,5,-13,4">
<ListBox.Resources>
<ResourceDictionary>
<System:Boolean x:Key="YesValue">True</System:Boolean>
<System:Boolean x:Key="NoValue">False</System:Boolean>
</ResourceDictionary>
</ListBox.Resources>
<ListBox.SelectedValue>
<Binding
Mode="TwoWay"
Path="IsValidateQuestion2"
UpdateSourceTrigger="PropertyChanged">
</Binding>
</ListBox.SelectedValue>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<RadioButton
Margin="0,0,10,0"
Content="{TemplateBinding Content}"
IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBoxItem Tag="{StaticResource YesValue}">Yes</ListBoxItem>
<ListBoxItem Tag="{StaticResource NoValue}">No</ListBoxItem>
</ListBox>
<Line Grid.Row="2" Grid.Column="4" X1="0" Y1="20" Stroke="Red" StrokeThickness="4" >
<Line.Style>
<Style TargetType="Line">
<Setter Property="Visibility" Value="Visible"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsValidateQuestion3}" Value="true">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsValidateQuestion3}" Value="false">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Line.Style>
</Line>
<ListBox
x:Name="Question3"
BorderThickness="0"
Grid.Column="4"
Grid.Row="2" SelectedValuePath="Tag" Margin="13,4,-12,5">
<ListBox.Resources>
<ResourceDictionary>
<System:Boolean x:Key="YesValue">True</System:Boolean>
<System:Boolean x:Key="NoValue">False</System:Boolean>
</ResourceDictionary>
</ListBox.Resources>
<ListBox.SelectedValue>
<Binding
Mode="TwoWay"
Path="IsValidateQuestion3"
UpdateSourceTrigger="PropertyChanged">
</Binding>
</ListBox.SelectedValue>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<RadioButton
Margin="0,0,10,0"
Content="{TemplateBinding Content}"
IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBoxItem Tag="{StaticResource YesValue}">Yes</ListBoxItem>
<ListBoxItem Tag="{StaticResource NoValue}">No</ListBoxItem>
</ListBox>
<Line Grid.Row="3" Grid.Column="4" X1="0" Y1="20" Stroke="Red" StrokeThickness="4" >
<Line.Style>
<Style TargetType="Line">
<Setter Property="Visibility" Value="Visible"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsValidateQuestion4}" Value="true">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsValidateQuestion4}" Value="false">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Line.Style>
</Line>
<ListBox
x:Name="Question4"
BorderThickness="0"
Grid.Column="4"
Grid.Row="3"
SelectedValuePath="Tag"
Margin="13,5,-13,4" RenderTransformOrigin="1.133,0.5" HorizontalAlignment="Left" Width="90">
<ListBox.Resources>
<ResourceDictionary>
<System:Boolean x:Key="YesValue">True</System:Boolean>
<System:Boolean x:Key="NoValue">False</System:Boolean>
</ResourceDictionary>
</ListBox.Resources>
<ListBox.SelectedValue>
<Binding
Mode="TwoWay"
Path="IsValidateQuestion4"
UpdateSourceTrigger="PropertyChanged">
</Binding>
</ListBox.SelectedValue>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<RadioButton
Margin="0,0,10,0"
Content="{TemplateBinding Content}"
IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBoxItem Tag="{StaticResource YesValue}">Yes</ListBoxItem>
<ListBoxItem Tag="{StaticResource NoValue}">No</ListBoxItem>
</ListBox>
</Grid>
</Window>
|
This is the partial class for view.
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for
MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var vm = new ViewModel.ValidationViewModel();
this.mygrid.DataContext = vm;
}
}
}
|
This piece of code will represent the view model. This code will used to perform the custom validation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApplication1.ViewModel
{
public class ValidationViewModel : ViewModelBase
{
private bool? _isValidateQuestion1 = null;
private bool? _isValidateQuestion2 = null;
private bool? _isValidateQuestion3 = null;
private bool? _isValidateQuestion4 = null;
public ValidationViewModel()
{
IsValidateQuestion1 = null;
}
public bool? IsValidateQuestion1
{
get { return _isValidateQuestion1; }
set
{
_isValidateQuestion1 = value;
NotifyPropertyChanged("IsValidateQuestion1");
}
}
public bool? IsValidateQuestion2
{
get { return _isValidateQuestion2; }
set
{
_isValidateQuestion2 = value;
NotifyPropertyChanged("IsValidateQuestion2");
}
}
public bool? IsValidateQuestion3
{
get { return _isValidateQuestion3; }
set
{
_isValidateQuestion3 = value;
NotifyPropertyChanged("IsValidateQuestion3");
}
}
public bool? IsValidateQuestion4
{
get { return _isValidateQuestion4; }
set
{
_isValidateQuestion4 = value;
NotifyPropertyChanged("IsValidateQuestion4");
}
}
}
}
|
0 comments:
Please let me know your view