Gcobani Mkontwana

Gcobani Mkontwana

  • 560
  • 1.9k
  • 410.9k

How to validate for an empty fields in wpf using c# class form?

Apr 26 2024 1:41 PM

Hi Team

I have this class name in xml, UCFinishedProduct.xml(UI), now when view source code its in c# i believe this will be my back end. Question now how will i integrate these logic together so my code works?  The logic is if value for both 'topseal and bottomseal' are empty during capturing, it must trigger these together so it prompt dialog message to a user, until these two captured correctly and focus be exit after.

<TextBox x:Name="txtTopSeal" HorizontalAlignment="Left" Foreground="Black"  Height="26" Margin="587,112,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="172" Text="{Binding TopSeal, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=true}">
            <Validation.ErrorTemplate>
                <ControlTemplate>
                    <DockPanel>
                        <Border BorderBrush="Red" BorderThickness="1">
                            <AdornedElementPlaceholder x:Name="textbox"/>
                        </Border>
                        <TextBlock Text="{Binding [0].ErrorContent}" Foreground="Red"/>
                    </DockPanel>
                </ControlTemplate>
            </Validation.ErrorTemplate>
        </TextBox>
        <Label x:Name="lblBottomSeal" Content="Bottom Seal:"  HorizontalAlignment="Left" Margin="486,143,0,0" VerticalAlignment="Top" Width="96" HorizontalContentAlignment="Right" Background="#FFE3E3E3" FontWeight="SemiBold" BorderThickness="1" BorderBrush="#FFBEBEBE"/>
        <TextBox x:Name="txtBottomSeal" HorizontalAlignment="Left" Foreground="Black"  Height="26" Margin="587,143,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="172" Text="{Binding BottomSeal, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=true}">
            <Validation.ErrorTemplate>
            <ControlTemplate>
                <DockPanel>
                    <Border BorderBrush="Red" BorderThickness="1">
                        <AdornedElementPlaceholder x:Name="textbox"/>
                    </Border>
                    <TextBlock Text="{Binding [0].ErrorContent}" Foreground="Red"/>
                </DockPanel>
            </ControlTemplate>
        </Validation.ErrorTemplate>
        </TextBox>
namespace Feedmill_Weighbridge.Presentation
{
    /// <summary>
    /// Interaction logic for UCFinishedProduct.xaml
    /// </summary>
    public partial class UCFinishedProduct : UserControl, IFinishedProductView, INotifyPropertyChanged, IDataErrorInfo
    {
        public FinishedProductPresenter presenter;
        private string _topSeal;
        private string _bottomSeal;
        List<Bin> objBinList;

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private EmptyRule _validates = new EmptyRule();

 


Answers (1)