locked
[UWP][MVVM] How to set width inside itemTeamplate inside layoutUpdate of listview? RRS feed

  • Question

  • How to set width inside itemTeamplate inside layoutUpdate of listview?
    Wednesday, December 30, 2015 1:25 AM

Answers

  • Hello,

    For example inside the ItemTemplate is the StackPanel, then in order to set the width inside itemTeamplate, first we need to use the VisualTreeHelper to help us to access the visual tree of the ListView to find the Stackpanel as following:

     public static DependencyObject MyFindListViewChildByName(DependencyObject parant, string ControlName)
            {
                int count = VisualTreeHelper.GetChildrenCount(parant);
    
                for (int i = 0; i < count; i++)
                {
                    var MyChild = VisualTreeHelper.GetChild(parant, i);
                    if (MyChild is FrameworkElement && ((FrameworkElement)MyChild).Name == ControlName)
                        return MyChild;
    
                    var FindResult = MyFindListViewChildByName(MyChild, ControlName);
                    if (FindResult != null)
                        return FindResult;
                }
    
                return null;
            }
    After that inside the LayoutUpdated method, we can set the width as following:
    StackPanel MyStackPanel = MyFindListViewChildByName(MyListView, "MyStackPanel") as StackPanel;
    MyStackPanel.Width = 200;
    Best Regards,
    Amy Peng



    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.

    Wednesday, December 30, 2015 5:44 AM