Suppose you are using MVVM (this is not what you are doing now), and that
ItemsSource="{Binding DList}"
is the correct binding to a set of models
You will need
DisplayMemberPath="Number"
Let's get back to your question. First write another anchor for the selected text
Text="{Binding Selected, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors =true, NotifyOnValidationError=true}"
and embed a validation tooltip inside the combo
ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
and style in window resources
<Window.Resources> <Style TargetType="{x:Type Label}"> <Setter Property="Margin" Value="5,0,5,0" /> <Setter Property="HorizontalAlignment" Value="Right" /> </Style> <Style TargetType="{x:Type ComboBox}"> <Setter Property="VerticalAlignment" Value="Center" /> <Setter Property="Margin" Value="0,2,40,2" /> <Setter Property="Validation.ErrorTemplate"> <Setter.Value> <ControlTemplate> <DockPanel LastChildFill="true"> <Border Background="Red" DockPanel.Dock="right" Margin="5,0,0,0" Width="20" Height="20" CornerRadius="10" ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"> <TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="white"> </TextBlock> </Border> <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" > <Border BorderBrush="red" BorderThickness="1" /> </AdornedElementPlaceholder> </DockPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources>
Finally, we must check the ViewModel property
We can project a list of models on our numbers to check for errors.
public class VM : IDataErrorInfo { public string this[string columnName] { get { if (columnName.Equals( "Selected")) { if (!DList.Select(m => m.Number).Contains(Selected)) return "Selected number must be in the combo list"; } return null; } }
You can learn more about data validation in MVVM, for example here
Redeem verification later
Suppose you want to test after clicking save
<Button Content="Save" Command="{Binding SaveCmd}"
you just need to raise the property changed in the corresponding delegate command
public class VM : ViewModelBase, IDataErrorInfo { private bool showValidation; private int selected; public int Selected { get { return selected; } set { selected = value; showValidation = true; OnPropertyChanged("Selected"); } } DelegateCommand saveCmd; public ICommand SaveCmd { get { if (saveCmd == null) { saveCmd = new DelegateCommand(_ => RunSaveCmd(), _ => CanSaveCmd()); } return saveCmd; } } private bool CanSaveCmd() { return true; } private void RunSaveCmd() { showValidation = true; OnPropertyChanged("Selected"); }
and exit the check before you want to show it.
public string this[string columnName] { get { if (!showValidation) { return null; }