0

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.

how it looks

/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).

Mikaèl
  • 233
  • 3
  • 13
  • I'm not sure if I've misunderstood what you are trying to do, or if you haven't understood how to bind correctly. In your letterinrows user control you've set bind in the letter property, but I can't see where you've actually set what is it meant to bound too?. Also In the letterControl xaml, I can't see any bindings. I'm guessing your trying to set the ContainsLetters property into the textblock in the letterControl? – JayDev Jul 15 '14 at 15:49
  • It is something like ListBox of textboxes (Listbox is LetterInRowUserControl and textboxes are LetterControl). ListBox has one string property - ContainsLetters and single letters from this string divide to many textboxes. – Mikaèl Jul 15 '14 at 15:58
  • Ah I think I see where you are going wrong. You are attempting to update on the Register method. I believe this will only be called when you are creating the dependency property, not whenever the property is updated. Try using a changed event such as the one used in this thread. http://stackoverflow.com/questions/20080902/dependency-property-assigned-with-value-binding-does-not-work – JayDev Jul 15 '14 at 16:09
  • It didn't help (I tried both cases) - it's a same thing - only that I'm using anonymous of this method. – Mikaèl Jul 15 '14 at 16:23

0 Answers0