【问题标题】:Access Items in DataTemplate in Windows Phone 8.1在 Windows Phone 8.1 中访问 DataTemplate 中的项目
【发布时间】:2015-04-27 04:42:35
【问题描述】:

我正在编写一个 Windows Phone 8.1 (WINRT) 应用程序。

我有一个带有 datatemplate 的 listbox。在数据模板中,我有一个网格和一个文本块。 如何在c#中设置网格或文本块的宽度?

我尝试使用 Internet 上的 DependencyObject VisualTreeHelper 方法,但它不起作用。

XAML:

<SemanticZoom x:Name="CategorySemanticZoom" 
                                          IsZoomOutButtonEnabled="True"
                                          CanChangeViews="True"
                                           Grid.Row="1">
                                <SemanticZoom.ZoomedInView>
                                    <ListView x:Name="Category_ListViewDetail" IsSwipeEnabled="True" IsTapEnabled="True"
                                              IsItemClickEnabled="True"
                                              IsZoomedInView="True" 
                                             ItemClick="Category_ListViewDetail_ItemClick" >
                                        <ListView.ItemTemplate>
                                            <DataTemplate>
                                                <Grid x:Name="SubCategoryName_grid"
                                                      x:FieldModifier="public"
                                                      >

                                                    <TextBlock
                                                        Text="{Binding SubCategoryName}" 
    x:Name="SubCategoryName_TextBlock"
                                                        FontSize="26"
                                                        Margin="30,0,10,0"                                           
                                                        TextTrimming="CharacterEllipsis"/>
                                                </Grid>
                                            </DataTemplate>
                                        </ListView.ItemTemplate>
                                        <ListView.GroupStyle>
                                            <GroupStyle>
                                                <GroupStyle.HeaderTemplate>
                                                    <DataTemplate>
                                                        <Border Background="{StaticResource DefaultTheme_DarkBlueColor}" 
                                                                CornerRadius="6"
                                                                HorizontalAlignment="Left"
                                                                Margin="10,20,10,20" 
                                                                Tapped="Border_Tapped"
                                                                VerticalAlignment="Center">
                                                            <TextBlock Text="{Binding CategoryName}" 
                                                                     />
                                                        </Border>
                                                    </DataTemplate>
                                                </GroupStyle.HeaderTemplate>
                                                <GroupStyle.Panel>
                                                    <ItemsPanelTemplate>
                                                        <VariableSizedWrapGrid Orientation="Vertical"
                                                                               Margin="0 0 0 0"
                                                                               ItemHeight="55"/>
                                                    </ItemsPanelTemplate>
                                                </GroupStyle.Panel>
                                            </GroupStyle>
                                        </ListView.GroupStyle>
                                        <ListView.ItemsPanel>
                                            <ItemsPanelTemplate>
                                                <StackPanel Orientation="Vertical" />
                                            </ItemsPanelTemplate>
                                        </ListView.ItemsPanel>
                                    </ListView>
                                </SemanticZoom.ZoomedInView>
                                <SemanticZoom.ZoomedOutView>
                                    <ListView x:Name="Category_ListViewSummary"
                                              Background="LightGray" 
                                              IsZoomedInView="False"
                                              Opacity=".85">
                                        <ListView.ItemTemplate>
                                            <DataTemplate>
                                                <Border Background="{StaticResource DefaultTheme_DarkBlueColor}" 
                                                                CornerRadius="6"
                                                                HorizontalAlignment="Left"
                                                                Margin="10,10,10,10" >

                                                    <TextBlock Text="{Binding  Group.CategoryName}" 
                                                                    />
                                                </Border>


                                            </DataTemplate>
                                        </ListView.ItemTemplate>
                                        <ListView.ItemsPanel>
                                            <ItemsPanelTemplate>
                                                <StackPanel Orientation="Vertical"/>
                                            </ItemsPanelTemplate>
                                        </ListView.ItemsPanel>
                                    </ListView>
                                </SemanticZoom.ZoomedOutView>
                            </SemanticZoom>

如何在 C# 中设置 SubCategoryName_grid 或 SubCategoryName_TextBlock 的宽度?

【问题讨论】:

  • 我知道它没有多大帮助,但是为什么你必须在 c# 中这样做呢?为什么不在作为您的项目的每个模型上定义一个属性(例如,具有 SubCategoryName 的同一类,然后实现 IValueConverter?
  • 我没有使用 MVVM/MVC :(
  • 你有{Binding},对吧?所以你一定会做些什么?
  • 我尝试使用 SubCategoryName_TextBlock 的宽度为: Width="{Binding WidthSet}" 并设置 double WidthSet=380;在 c# 代码中,但它不起作用。我做错了吗?

标签: c# windows xaml windows-runtime windows-phone-8.1


【解决方案1】:
 DependencyObject findElementInItemsControlItemAtIndex(ItemsControl itemsControl,
                                                              int itemOfIndexToFind,
                                                              string nameOfControlToFind)
        {
            if (itemOfIndexToFind >= itemsControl.Items.Count) return null;

            DependencyObject depObj = null;
            object o = itemsControl.Items[itemOfIndexToFind];
            if (o != null)
            {
                var item = itemsControl.ItemContainerGenerator.ContainerFromItem(o);
                if (item != null)
                {
                    depObj = getVisualTreeChild(item, nameOfControlToFind);
                    return depObj;
                }
            }
            return null;
        }


        DependencyObject getVisualTreeChild(DependencyObject obj, String name)
        {
            DependencyObject dependencyObject = null;
            int childrenCount = VisualTreeHelper.GetChildrenCount(obj);
            for (int i = 0; i < childrenCount; i++)
            {
                var oChild = VisualTreeHelper.GetChild(obj, i);
                var childElement = oChild as FrameworkElement;
                if (childElement != null)
                {
                    //Code to take care of Paragraph/Run
                    if (childElement is RichTextBlock || childElement is TextBlock)
                    {
                        dependencyObject = childElement.FindName(name) as DependencyObject;
                        if (dependencyObject != null)
                            return dependencyObject;
                    }

                    if (childElement.Name == name)
                    {
                        return childElement;
                    }
                }
                dependencyObject = getVisualTreeChild(oChild, name);
                if (dependencyObject != null)
                    return dependencyObject;
            }
            return dependencyObject;
        }


        private void Category_ListViewDetail_Loaded(object sender, RoutedEventArgs e)
        {
            ListView ListViewObject = sender as ListView;

            //For changing Detail Sub Categories Width:

            int CategoriesCount = ListViewObject.Items.Count;

            for (int i = 0; i < CategoriesCount; i++)
            {
                TextBlock SubCategory_TextBlockObject = findElementInItemsControlItemAtIndex(ListViewObject, i, "SubCategoryName_TextBlock") as TextBlock;
                if (SubCategory_TextBlockObject != null)
                {
                    SubCategory_TextBlockObject.Width = Window.Current.Bounds.Width - 50;

                }
            }

            for (int i = 0; i < CategoriesCount; i++)
            {
                TextBlock Category_TextBlockObject = findElementInItemsControlItemAtIndex(ListViewObject, i, "CategoryName_InDetailView_Textblock") as TextBlock;
                if (Category_TextBlockObject != null)
                {
                    Category_TextBlockObject.Width = Window.Current.Bounds.Width - 30;

                }
            }



        }


        private void Category_ListViewSummary_Loaded(object sender, RoutedEventArgs e)
        {
            //For changing Summary Categories Width:

            ListView ListViewObject = sender as ListView;

            int CategoriesCount = GetCategoriesResultObject.MasterCategories[0].Categories.Count;

            for(int i=0;i<CategoriesCount;i++)
            {
                TextBlock Category_TextBlockObject = findElementInItemsControlItemAtIndex(ListViewObject, i, "CategoryName_Textblock") as TextBlock;
                    if (Category_TextBlockObject != null)
                    {
                        Category_TextBlockObject.Width = Window.Current.Bounds.Width - 30;
                    }
            }

        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多