How to use condition to compare two things for dropdown and an empty field in wpf?

Gcobani Mkontwana 60 Reputation points
2024-05-06T20:20:32.45+00:00

Hi Team

Need to use a condition will check if combo box(truck-empty) for selected item and when an empty field(topseal and bottomseal), needs to validate for both. Meaning when a user selects this combo box for truck-empty condition must met only here, for when checking if bottomseal and topseal is empty must trigger dialog message for empty and put focus.

<``Label ``x:``Name``="``lblPurpose``"`` ``Content="``Purpose:``"`` ``HorizontalAlignment="``Left``"`` ``Margin="``12,141,0,0``"`` ``VerticalAlignment="``Top``"`` ``Width="``93``"`` ``HorizontalContentAlignment="``Right``"`` ``Background="``#FFE3E3E3``"`` ``FontWeight="``SemiBold``"`` ``BorderThickness="``1``"`` ``BorderBrush="``#FFBEBEBE``"/> <``ComboBox ``x:``Name``="``cbxPurpose``"`` ``HorizontalAlignment="``Left``"`` ``Margin="``110,142,0,0``"`` ``VerticalAlignment="``Top``"`` ``Width="``119``"`` ``Height="``27``"/> <``Label ``x:``Name``="``lblCompartment``"`` ``Content="``Compartment:``"`` ``HorizontalAlignment="``Left``"`` ``Margin="``12,171,0,0``"`` ``VerticalAlignment="``Top``"`` ``Width="``93``"`` ``HorizontalContentAlignment="``Right``"`` ``Background="``#FFE3E3E3``"`` ``FontWeight="``SemiBold``"`` ``BorderThickness="``1``"`` ``BorderBrush="``#FFBEBEBE``"/> <``ComboBox ``x:``Name``="``cbxCompartment``"`` ``IsEnabled="``True``"`` ``IsEditable="``True``"`` ``HorizontalAlignment="``Left``"`` ``Margin="``110,170,0,0``"`` ``VerticalAlignment="``Top``"`` ``Width="``119``"`` ``Height="``26``"/>

private void btnSaveReturn_Click(object sender, RoutedEventArgs e)

{

// Debugging output

Console.WriteLine("Selected compartment: " + (cbxCompartment.SelectedItem != null ? cbxCompartment.SelectedItem.ToString() : "null"));

Console.WriteLine("Selected purpose: " + (cbxPurpose.SelectedItem != null ? cbxPurpose.SelectedItem.ToString() : "null"));

// Check if Compartment is truck-full and Purpose is Leaving FinishedProduct

if (cbxCompartment.SelectedItem != null && cbxCompartment.SelectedItem.ToString() == "truck-full"

&& cbxPurpose.SelectedItem != null && cbxPurpose.SelectedItem.ToString() == "Leaving Finished Product")

{

// Validate top seal

if (string.IsNullOrEmpty(txtRTopSeal.Text))

{

MessageBox.Show("TopSeal cannot be empty when truck is full", "Validation Error");

txtRTopSeal.Focus();

return;

}

// Validate bottom seal

if (string.IsNullOrEmpty(txtRBottomSeal.Text))

{

MessageBox.Show("BottomSeal cannot be empty when truck is full", "Validation Error");

txtRBottomSeal.Focus();

return;

}

}

}

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,700 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,521 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 47,101 Reputation points Microsoft Vendor
    2024-05-07T09:40:08.9366667+00:00

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful