Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4104898
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:10:37+00:00 2026-05-20T21:10:37+00:00

I new to WPF and C# and I have a problem with my application.

  • 0

I new to WPF and C# and I have a problem with my application. I have a TextBox which I want to have a ValidationRule on to validate the text. Now I want to have a default value in the TextBox but i can’t figure out how to do it. I’ve tried alot of ways and the tips I find when googling the problem doesn’t seem to work at all.

Also is there any way to do this without the use of an ProjectData class file? To me it seems wierd to have to make a class with just one value to be able to achieve validation.

My ValidationRule looks like this:

public class OpcValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            string source = (string)value;
            if(!source.StartsWith("Test")) 
            {
                return new ValidationResult(false, msg);
            }


            // Valid!!!!
            return new ValidationResult(true, null);
        }
    }

My TextBox looks like this:

<TextBox x:Name="OPCAddressBox" Style="{StaticResource textBoxInError}" HorizontalAlignment="Right" TextWrapping="NoWrap" VerticalAlignment="Top" Margin="0,10,8,0" Width="150">
                    <TextBox.Text>
                        <Binding Path="OpcServerAddress" Source="{StaticResource pdd}" UpdateSourceTrigger="PropertyChanged">
                            <Binding.ValidationRules>
                                <local:OpcValidationRule />
                            </Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>

My Resources looks like this:

<Window.Resources>
        <local:ProjectData Height="1000" Width="1000" OpcServerAddress="opc.tcp://address:port" x:Key="pdd"/>

        <Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

My ProjectData file looks like this:

public class ProjectData
    {
        private string opcServerAddress;
        public string OpcServerAddress
        {
            get { return opcServerAddress; }
            set { opcServerAddress = value; }
        }

        public ProjectData()
        {
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:10:37+00:00Added an answer on May 20, 2026 at 9:10 pm

    You have to know that usually, if you want to implement a WPF application the “correct” way, your XAMLs will be bound to a ViewModel, keeping the properties.
    I know it seems kinda heavy for the small amount of property you have to keep here, but believe me, it’s awesome when you have bigger UIs.
    You can also look around about Triggers which can help you validate something without adding a class (but would be VERY heavy if you have many fields to validate)
    I’d advise you to check out this article which really helped me learn more about validation in WPF, if you haven’t already read it:

    http://www.codeproject.com/KB/WPF/wpfvalidation.aspx

    EDIT

    For the default value:

    Your Project data class has to implement the interface “INotifyPropertyChanged”
    This allows to fire an event each time the text is changed, and therefore update the binding. Once you have done that (I’d encourage you to look around using Google, unfortunately I don’t have any specific article to suggest, but you’ll find a nice one for sure), just initialize your string in the constructor, something like that:

    public ProjectData()
            {
                opcServerAddress = "Hello!";
            }
    

    Then, thanks to the binding, the textbox will have the default value you just specified, and each time you modify it, opcServerAddress value will be updated following the value in the text box.
    This will especially allow you to use this string in the ProjectData class (commonly called ViewModel, if you have time, check out the MVVM model:
    Need good MVVM tutorial for WPF

    MVVM: Tutorial from start to finish?
    it is very useful, and it is considered to be the “proper” way to work in WPF)

    Have fun! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem in a WPF application. I wrote this code: public partial
I using multilanguage solution from this thread Multilanguage in WPF But i have problem
I am new to WPF and I am creating an application which uses the
I'm new to WPF and come from a WinForms background and have a fairly
I'm considering integrating some D3D code I have with WPF via the new D3DImage
I am fairly new to WPF and I am having a problem with inheriting
I'm new to WPF and data binding so hopefully I can explain the problem
Ok... I'm new to WPF, but I kind of know how to do things
I have the following problem in wpf: I have defined a user control (in
We have a very strange behaviour of a WPF application. Sometimes (not very ofen),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.