|
Hi i have a simple text box in window. I just want to write a simple validation for the text box.
If there is nothing in the text box it should show a validation error. or else nothing.
Can any one please provide the code for this.. Full XAML file. and Cs file
Santhapur
|
|
|
|
|
Santhapur wrote: Can any one please provide the code for this.. Full XAML file. and Cs file
That's not the way this site works you know. We don't actually write your code for you.
|
|
|
|
|
Thanks for your Reply. I have asked u the full code b/c previously some one posted code snippet and its not working.. So i am not able to find where the error was..
I am not going to earn money with u r code.. i am still in learning stage.
Pete O'Hanlon wrote: That's not the way this site works you know. We don't actually write your code for you.
|
|
|
|
|
Asking for full code breaches the spirit of the site - and you'll get short shrift for it. We give you help, and pointers, we don't write the code. If you're given a snippet and it doesn't work, reply to the poster and ask for clarification - that's the normal etiquette. You'll see that most of the solutions in the forums, are in the form of snippets or advice on where to look. That's just the way it is - because you will learn best by actually solving the problem for yourself.
|
|
|
|
|
Here is what i have tried..
<Window x:Class="TextBoxValidation.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:y="http://schemas.microsoft.com/winfx/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TextBox>
<TextBox.Text>
<Binding.ValidationRules>
<local:MatchRule MatchText="Hello" />
</Binding.ValidationRules>
</TextBox.Text>
</TextBox>
</Grid>
</Window>
namespace TextBoxValidation
{
class validatetextbox : ValidationRule
{
public string MatchText { get; set; }
public override ValidationResult Validate(object value,
CultureInfo info)
{
if ((string)value == MatchText)
return new ValidationResult(true, null);
return new ValidationResult(false, "This is not a match");
}
}
}
Here are my results..
http://schemas.microsoft.com/winfx/2006/xaml/presentation'. Line 9 Position 18
|
|
|
|
|
Well, the first thing I can see is that you've called the class validatetextbox , but it's referred to as MatchRule . Rename the class to MatchRule . Also, I don't see a reference to xmlns:local in the Window declaration.
|
|
|
|
|
xmlns:local="clr-namespace:TextBoxValidation"
<local:validatetextbox MatchText="Hello" />
I have included these two line but still getting the same error
|
|
|
|
|
The reason you are getting this error is because you haven't specified valid XAML code. Basically, you want to change your XAML so that you are binding your textbox to something, and set your validation rule on it like so:
<TextBox>
<Binding Path="Name">
<Binding.ValidationRules>
<local:MatchRule MatchText="Hello" />
</Binding.ValidationRules>
</Binding>
</TextBox> Take a look at this[^] article for a detailed example.
|
|
|
|
|
Santhapur wrote: Can any one please provide the code for this
No but I will give a hint, you are looking for ValidationRule
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns
|
|
|
|
|
Hi.
How to handle Tab Index changing in TabControl ?
Thanks in advance.
|
|
|
|
|
There is a SelectionChanged event that you can handle.
modified 27-Feb-21 21:01pm.
|
|
|
|
|
Thanks Karl.
But how can I get Selected tab index ?
|
|
|
|
|
TabControl is like other ItemsControls. It has SelectedIndex, SelectedItem, and SelectedValue properties that you can use in your code, depending on what you are looking to do.
modified 27-Feb-21 21:01pm.
|
|
|
|
|
Thanks Karl for a nice and quick response.
In WinForm we can get TebIndex :
void tabControl1_Selected(object sender, TabControlEventArgs e)
{
}
But in WPF I didn't find a thing like that :
private void TabControl_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
}
|
|
|
|
|
That is the event you need to handle.
modified 27-Feb-21 21:01pm.
|
|
|
|
|
Thanks Karl.
Karl Shifflett wrote: That is the event you need to handle.
Yes, I understand.
But I want to get new SelectedIndex of tab when SelectedIndex changed ?
Do you understand what I mean ?
|
|
|
|
|
The below code assumes that my TabControl is named tcDemo.
You can also use the e.AddedItems property to get a list of selected items.
The way I'm doing it, is more direct.
Private Sub tcDemo_SelectionChanged(ByVal sender As Object, _
ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles tcDemo.SelectionChanged
Dim objTabItem As TabItem = CType(Me.tcDemo.SelectedItem, TabItem)
End Sub
modified 27-Feb-21 21:01pm.
|
|
|
|
|
Thanks anyway.
I'm working C# and I couldn't understand your VB code well.
But I used this code and my problem solved.
void TabControl_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
MessageBox.Show(((TabControl)sender).SelectedIndex.ToString());
}
|
|
|
|
|
|
Wow
Great web site.
Thanks Karl.
I really need a something like it.
|
|
|
|
|
Me too! It helps me out a good bit. Glad it was helpful!
modified 27-Feb-21 21:01pm.
|
|
|
|
|
Hi!
I'm trying to create a small program that allows to create,edit and print several slides. Idea is similar to PowerPoint, although much much simpler (only editable RichText and Images from files placed on the slide)
How can I do this? (FlowDocument won't work because fixed coords are necessary for all the elements)
I can think of using Canvas layout, dynamically destroying old controls(Saving the edited text and links to images to some internal structure) and creating-placing new EditText and Image controls each time user clicks Next or Previous slide buttons(filling them with data from saved internal data).
But how do I allow zooming of this canvas with controls so that if user zooms in, scroll bars appear, text and image controls enlarge correctly like in any Office App.
Will I be able then to print this canvas with all the controls on it?
Is there a better way to do this?
Thanks!
|
|
|
|
|
Have you tried the RTF control? There is a great example in the SDK examples.
modified 27-Feb-21 21:01pm.
|
|
|
|
|
Hi All,
I'm currently implementing some commands on Views / Controllers in a WPF app but I'm seeing something I didn't expect with the DelegateCommand. My Can<insert command name> only gets called once during the initial 'setup' of the view. So for instance the app gets loaded, all the Can whatever commands get called and setup various things, but when a command is raised and the state of the return values from these CanExecute switch the UI doesn't respond ... I expected them to operate more like 'normal' WPF command bindings.
Anyone have any ideas?
|
|
|
|
|
Fixed this!
|
|
|
|