I have one UserControl LetterInRowUserControl with LongListSelector and ItemTemplate in xaml part
<UserControl>
...
<UserControl.Resources>
<DataTemplate x:Key="itemTemplate">
<local:LetterControl x:Name="letterControl" VerticalAlignment="Top" Width="50" Letter="{Binding}"/>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<phone:LongListSelector x:Name="lls" LayoutMode="List" ItemTemplate="{StaticResource itemTemplate}" />
</Grid>
</UserControl>
cs part:
public partial class LetterInRowUserControl : UserControl
{
public LetterInRowUserControl()
{
InitializeComponent();
LetterControl.TextChanged += (o, ev) =>
{
var conv = new StringToListConverter();
ContainsLetters = (string)conv.ConvertBack(lls.ItemsSource, typeof(string), null, CultureInfo.CurrentCulture);
};
}
public static readonly DependencyProperty ContainsLettersProperty =
DependencyProperty.Register("ContainsLetters", typeof (string), typeof (LetterInRowUserControl), new PropertyMetadata(default(string),
(o, args) =>
{
var control = (LetterInRowUserControl) o;
var conv = new StringToListConverter();
control.lls.ItemsSource = (ObservableCollection<string>)conv.Convert(args.NewValue, typeof(string), null, CultureInfo.CurrentCulture);
}));
public string ContainsLetters
{
get
{
return (string) GetValue(ContainsLettersProperty);
}
set
{
SetValue(ContainsLettersProperty, value);
}
}
}
then I have second Usercontrol - LetterControl which is inside itemstemplate of LetterInRowUserControl
xaml part:
<UserControl>
...
<Grid x:Name="LayoutRoot">
<Grid>
<Button x:Name="hidden" Height="0" Width="0"/>
<TextBox x:Name="textBox"
TextWrapping="Wrap" VerticalAlignment="Top" Height="84" Margin="-12" FontFamily="Segoe WP Semibold" FontSize="33.333" TextAlignment="Center" GotFocus="textBox_GotFocus" LostFocus="textBox_LostFocus" MaxLength="1" TextChanged="textBox_TextChanged"/>
</Grid>
</Grid>
</UserControl>
and cs part:
public partial class LetterControl : UserControl
{
public static event TextChangedEventHandler TextChanged;
public static readonly DependencyProperty LetterProperty =
DependencyProperty.Register("Letter", typeof(string), typeof(LetterControl), new PropertyMetadata(default(string),
(o, args) =>
{
var control = (LetterControl)o;
control.textBox.Text = (string)args.NewValue;
}));
public string Letter
{
get { return (string)GetValue(LetterProperty); }
set
{
SetValue(LetterProperty, value);
}
}
public LetterControl()
{
InitializeComponent();
}
private void textBox_GotFocus(object sender, RoutedEventArgs e)
{
textBox.SelectAll();
}
private void textBox_LostFocus(object sender, RoutedEventArgs e)
{
textBox.Text = textBox.Text.ToUpper();
}
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
SetValue(LetterProperty,textBox.Text);
if(textBox.Text.Length>=1)
hidden.Focus();
if (TextChanged != null) TextChanged.Invoke(sender, e);
}
}
Basically there are two UserControls: LetterInRowUserControl and LetterControl.
LetterInRowUserControl has LongListSelector with LetterControl inside its ItemTemplate.
The problem is that if I change Property Letter from inside LetterControl, it doesn't affect ItemSource in LongListSelector in LetterInRowUserControl. Conversely (if I change ItemSource directly) it works without problems.
/intended to one Windows Phone 8 App
... something like listbox with textboxes of one letter - problem is that if I edit a letter ItemSource of listbox doesn't change.
EDIT: After hard trying I have succeeded. And my code is working. Main problem was that I was using ObservableCollection<string>
and not ObservableCollection<CustomObjectWithINotifyPropChangedImplementation>.
So the value wasn't automatically changed (ObservableCollection did not fulfill its function).