|
If you can give me a small ready-to-run sample I might be able to help you. We have lots of Winforms/Wpf interaction and I remember that it was quite a challenge to get the focus right. You might want to try to set the focus to your element via a dispatcher like so:
Dispatcher.Invoke(new Action(() => Focus()), System.Windows.Threading.DispatcherPriority.Background);
this might also help:
http://geekswithblogs.net/rakker/archive/2007/07/27/114232.aspx[^]
|
|
|
|
|
Is there a control anywhere that is similar to the page that you see when you make a new message on this website?
|
|
|
|
|
Ummmm, is this WPF related at all?
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
I would like to bind the SelectedIndex property of ComboBox to a property that is defined in my class that extends the Window class. It is hard to explain what exactly I want, so I will try to explain it with a simple example:
here is my .xaml code:
<Window x:Class="WpfApplication3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ComboBox Grid.Row="0" x:Name="cmbSource" SelectionChanged="cmbSource_SelectionChanged">
<ComboBox.Items>
<ComboBoxItem> 1 </ComboBoxItem>
<ComboBoxItem> 2 </ComboBoxItem>
</ComboBox.Items>
</ComboBox>
<ComboBox Grid.Row="1" x:Name="cmbDestination" SelectedIndex="{Binding ElementName=Item, Path=Index}"> <!-- this doesn't work -->
<ComboBox.Items>
<ComboBoxItem>Item 1</ComboBoxItem>
<ComboBoxItem>Item 2</ComboBoxItem>
</ComboBox.Items>
</ComboBox>
</Grid>
</Window>
and here is my code-behind code:
namespace WpfApplication3
{
public partial class Window1 : Window
{
private Test _item;
public Test Item
{
get{return this._item;}
set{this._item = value;}
}
public Window1()
{
InitializeComponent();
}
private void cmbSource_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Item = new Test(cmbSource.SelectedIndex);
}
}
public class Test
{
private int _index;
public int Index
{
get { return this._index; }
set { this._index = value; }
}
public Test(int index)
{
Index = index;
}
}
}
When I was tring to solve this problem, I extended my Window1 class so it implements INotifyPropertyChanged interface, than I extended the Test class the same way, but nothing works. I even tried to define the Item Property as a DependencyProperty, but that didn't work either. Any idea will be appreciated.
thanks
Uros
|
|
|
|
|
You need to change your "Binding ElementName=Item, Path=Index"
First, add a x:Name property to your Window.
Then change the ElementName to the Window.Name property value.
Then change the Path to Item.Index
modified 27-Feb-21 21:01pm.
|
|
|
|
|
Yes that solved my problem. Thank you,
Uros
|
|
|
|
|
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());
}
|
|
|
|
|