Monday, October 27, 2014

Duell of the controls, which event fires first?

Hi Torsten,


I have modified the two aspects:



  1. I noticed that the TextBox will not lose focus when I click on ListBox after editing the text property, so I would recommend use TextChanged event of TextBox to work around.

  2. Created a new MyTextBox control to hold the current index of ListBox control. This would help to modify the ViewModel collection directly in code behind.


You can see the code snippet in the following.


In XAML.



<Page
x:Class="App49.MainPage"
xmlns=http://ift.tt/o66D3f
xmlns:x=http://ift.tt/mPTqtT
xmlns:local="using:App49"
xmlns:d=http://ift.tt/pHvyf2
xmlns:mc=http://ift.tt/pzd6Lm
mc:Ignorable="d">
<Page.Resources>
<local:PageViewModel x:Key="array1"></local:PageViewModel>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel>
<ListBox x:Name="liboEnergy" ItemsSource="{Binding Source={StaticResource array1}, Path=Collections}" VerticalAlignment="Top" Height="500" Margin="60,10,0,0" HorizontalAlignment="Left" Width="400">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Left" VerticalAlignment="Top">
<TextBlock Grid.Column="0" Height="30" FontSize="20" Text="{Binding Path=Text}" VerticalAlignment="Top" HorizontalAlignment="Right"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<local:MyTextBox x:Name="tbxEdit" Margin="60,20,0,0" Width="400" HorizontalAlignment="Left" Height="40" FontSize="22" Text="{Binding Path=SelectedItem.Text, ElementName=liboEnergy}" Index="{Binding ElementName=liboEnergy, Path=SelectedIndex}" TextChanged="tbxEdit_TextChanged"/>
</StackPanel>
</Grid>
</Page>

In XAML.cs.



public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
Random rnd = new Random();
for (int i = 0; i < 20; i++)
{
PageViewModel.Collections.Add(new DataLogic() { Text = "hello" + rnd.Next(0, 101), Index = i });
}
var data = liboEnergy.CacheMode;
}
private void tbxEdit_TextChanged(object sender, TextChangedEventArgs e)
{
MyTextBox textbox = sender as MyTextBox;
if (textbox != null && textbox.Index != -1)
{
PageViewModel.Collections[textbox.Index].Text = textbox.Text;
}
}
}
public class MyTextBox : TextBox
{
public int Index
{
get { return (int)GetValue(IndexProperty); }
set { SetValue(IndexProperty, value); }
}
public static readonly DependencyProperty IndexProperty =
DependencyProperty.Register(
"Index",
typeof(int),
typeof(MyTextBox),
new PropertyMetadata("", null));
}
public class PageViewModel
{
private static System.Collections.ObjectModel.ObservableCollection<DataLogic> list = new System.Collections.ObjectModel.ObservableCollection<DataLogic>();
public static System.Collections.ObjectModel.ObservableCollection<DataLogic> Collections
{
get { return list; }
}
}
public class DataLogic : INotifyPropertyChanged
{
private string _text;
public string Text
{
get
{
return this._text;
}
set
{
if (value != this._text)
{
this._text = value;
NotifyPropertyChanged();
}
}
}
public int Index { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}

The code snippets worked fine on my side, please let me know the result on my machine. If you still have questions, please feel free to let me know.


Regards,




We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.


No comments:

Post a Comment