0

Like for real, I do not get it, I have x:Name parameter in my StackPanel, but for whatever reason it's inaccesible in xaml.cs

Here is XAML:

<ListView x:Name="LV2" Background="#484848"
                          BorderBrush="Transparent"
                          ItemsSource="{Binding Lessons}"
                       ScrollViewer.VerticalScrollBarVisibility="Hidden" 
                      ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                      SizeChanged="LV2_SizeChanged">
                    <ListView.ItemTemplate>
                    <DataTemplate>
                        <ScrollViewer VerticalScrollBarVisibility="Hidden" Height="Auto" Width="Auto" Margin="0,0,20,0">
                            <StackPanel x:Name="SP" SizeChanged="SP_SizeChanged">
                                <TextBlock Width="Auto" FontSize="24" Foreground="Black" TextWrapping="Wrap">
                            <Run Text="{Binding LessonText}"/>
                                </TextBlock>
                                <Image Source="{Binding LessonImage}"/>
                            </StackPanel>
                        </ScrollViewer>
                    </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

And xaml.cs

namespace ApkaJezykowa.MVVM.View
{
  /// <summary>
  /// Logika interakcji dla klasy FrenchLessonView.xaml
  /// </summary>
  public partial class FrenchLessonView : UserControl
  {
    public FrenchLessonView()
    {
      InitializeComponent();
    }

    private void SP_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        SP.Height = HeightModel.Instance.Height;
        //That one doesn't work
    }

    private void LV2_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        LV2.Height = HeightModel.Instance.Height;
    }
  }
}

What is even the reason? I tried to do that with Scrollviewer too, result was the same, even removing other x:Name parameter (which works as it should).

kc99
  • 33
  • 6
  • 2
    Because it is in DataTemplate. https://stackoverflow.com/questions/16375375/how-do-i-access-a-control-inside-a-xaml-datatemplate You should be able to get indivisual instance from sender though. – emoacht Apr 30 '23 at 03:06
  • Each item has it's own name scope. If you think about it... That has to be the case. If you have 10 items you have ten stack panels which all have the same name. – Andy Apr 30 '23 at 12:14
  • I do not really get how to use proposed solution, but in the meantime I've realised that my method of resizing listview is terrible anyway,. So I've decided to mark provided question as an answer in case if anybody will happen to have similar problem as mine. – kc99 Apr 30 '23 at 16:02

0 Answers0