Hi,@Gcobani Mkontwana. You could try to refer to the code below. When debugging this code, you can enter the conditions required for top seal and bottom seal verification. It can also focus on the TextBox based on the conditions.
<Grid>
<Label x:Name="lblPurpose" Content="Purpose" HorizontalAlignment="Left" Margin="12,141,0,0" VerticalAlignment="Top"
Width="94" HorizontalContentAlignment="Right" Background="AliceBlue" BorderBrush="Black" BorderThickness="2"/>
<ComboBox Name="cbxPurpose" HorizontalAlignment="Left" Margin="110,142,0,0" VerticalAlignment="Top"
Width="119" Height="30" ItemsSource="{Binding PurposeList}" SelectedItem="{Binding SelectedPurpose, Mode=TwoWay}"/>
<Label x:Name="lblCompartment" Content="Compartment" HorizontalAlignment="Left" Margin="12,181,0,0" VerticalAlignment="Top"
Width="94" HorizontalContentAlignment="Right" Background="AliceBlue" BorderBrush="Black" BorderThickness="2"/>
<ComboBox Name="cbxCompartment" HorizontalAlignment="Left" Margin="110,182,0,0" VerticalAlignment="Top"
Width="119" Height="30" IsEnabled="True" IsEditable="True"
ItemsSource="{Binding CompartmentList}" SelectedItem="{Binding SelectedCompartment, Mode=TwoWay}"/>
<TextBox x:Name="txtRTopSeal" HorizontalAlignment="Left" Margin="110,222,0,0" VerticalAlignment="Top"
Width="120" Height="30" Text="{Binding TopSealText, UpdateSourceTrigger=PropertyChanged}" >
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding FocusOnTopSeal}" Value="True">
<Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<Label Content="Top Seal" HorizontalAlignment="Left" Margin="12,221,0,0" VerticalAlignment="Top"
Width="94" HorizontalContentAlignment="Right" Background="AliceBlue" BorderBrush="Black" BorderThickness="2"/>
<TextBox x:Name="txtRBottomSeal" HorizontalAlignment="Left" Margin="110,262,0,0" VerticalAlignment="Top"
Width="120" Height="30">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding FocusOnBottomSeal}" Value="True">
<Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<Label Content="Bottom Seal" HorizontalAlignment="Left" Margin="12,261,0,0" VerticalAlignment="Top"
Width="94" HorizontalContentAlignment="Right" Background="AliceBlue" BorderBrush="Black" BorderThickness="2"/>
<Button Content="Save Return" HorizontalAlignment="Left" Margin="110,302,0,0" VerticalAlignment="Top"
Width="120" Height="30" Click="btnSaveReturn_Click"/>
</Grid>
Codebehind:
public partial class MainWindow : Window
{
private readonly Stream stream;
public MainWindow()
{
InitializeComponent();
DataContext = new MainViewModel();
}
private void btnSaveReturn_Click(object sender, RoutedEventArgs e)
{
((MainViewModel)DataContext).ValidateAndSave();
}
}
public class MainViewModel : INotifyPropertyChanged
{
public List<string> PurposeList { get; set; }
private string _selectedPurpose;
public string SelectedPurpose
{
get { return _selectedPurpose; }
set
{
_selectedPurpose = value;
OnPropertyChanged(nameof(SelectedPurpose));
}
}
private string _selectedTopSeal;
public string SelectedTopSeal
{
get { return _selectedTopSeal; }
set
{
_selectedTopSeal = value;
OnPropertyChanged(nameof(SelectedTopSeal));
}
}
private string _selectedBottomSeal;
public string SelectedBottomSeal
{
get { return _selectedBottomSeal; }
set
{
_selectedBottomSeal = value;
OnPropertyChanged(nameof(SelectedBottomSeal));
}
}
public List<string> CompartmentList { get; set; }
private string _selectedCompartment;
public string SelectedCompartment
{
get { return _selectedCompartment; }
set
{
_selectedCompartment = value;
OnPropertyChanged(nameof(SelectedCompartment));
}
}
private bool _focusOnTopSeal;
public bool FocusOnTopSeal
{
get { return _focusOnTopSeal; }
set
{
_focusOnTopSeal = value;
OnPropertyChanged(nameof(FocusOnTopSeal));
}
}
private bool _focusOnBottomSeal;
public bool FocusOnBottomSeal
{
get { return _focusOnBottomSeal; }
set
{
_focusOnBottomSeal = value;
OnPropertyChanged(nameof(FocusOnBottomSeal));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public MainViewModel()
{
PurposeList = new List<string> { "Purpose 1", "Purpose 2", "Purpose 3", "Leaving Finished Product" };
CompartmentList = new List<string> { "Compartment 1", "truck-empty", "Compartment 2", "Compartment 3", "truck-full", "Compartment 1", "truck-empty", "Compartment 2", "Compartment 3", "truck-full" };
}
public void ValidateAndSave()
{
if (SelectedCompartment == "truck-full" && SelectedPurpose == "Leaving Finished Product")
{
if (string.IsNullOrEmpty(SelectedTopSeal))
{
MessageBox.Show("TopSeal cannot be empty when truck is full", "Validation Error");
FocusOnTopSeal = true;
return;
}
if (string.IsNullOrEmpty(SelectedBottomSeal))
{
MessageBox.Show("BottomSeal cannot be empty when truck is full", "Validation Error");
FocusOnBottomSeal = true;
return;
}
}
}
}
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.