【问题标题】:Xamarin Forms: Get control inside a listviewXamarin Forms:在列表视图中获取控制权
【发布时间】:2016-08-24 12:28:16
【问题描述】:

如何在此列表视图中获取网格 (x:Name="grd_containerline")。我必须能够得到孩子。 我尝试使用 this.FindByName("grd_containerline") 找到它,但不起作用。

有人可以帮我吗??

<Frame Style="{StaticResource StandardFrameStyle}" HorizontalOptions="FillAndExpand" VerticalOptions="EndAndExpand" IsVisible="{Binding IsContainerLineListViewVisible}">
      <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="FillAndExpand">
        <ListView x:Name="lst_containerline" ItemSelected="OnItemSelected" ItemsSource="{Binding ContainerLineList}" HasUnevenRows="True" Style="{StaticResource StandardListViewStyle}">
          <ListView.ItemTemplate>
            <DataTemplate>
              <ViewCell>
                <ViewCell.View>
                  <Grid x:Name="grd_containerline">
                    <Grid.RowDefinitions>
                      <RowDefinition Height="Auto" />
                      <RowDefinition Height="Auto" />
                      <RowDefinition Height="Auto" />
                      <RowDefinition Height="Auto" />
                      <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                      <ColumnDefinition Width="*" />
                      <ColumnDefinition Width="*" />
                      <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <Label x:Name="lbl_itemNo_binding_containerline" Text="{Binding ItemNo}" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" VerticalTextAlignment="Center" Style="{StaticResource StandardLabelStyle}" />

                    <Label x:Name="lbl_description_binding_containerline" Text="{Binding Description}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" VerticalTextAlignment="Center" Style="{StaticResource StandardLabelStyle}" />

                    <Label x:Name="lbl_lotNo_binding_containerline" Text="{Binding LotNo}" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" VerticalTextAlignment="Center" Style="{StaticResource StandardLabelStyle}" />

                    <Label x:Name="lbl_quantity_binding_containerline" Text="{Binding Quantity}" Grid.Row="3" Grid.Column="0" VerticalTextAlignment="Center" Style="{StaticResource StandardLabelStyle}" />
                    <Label x:Name="lbl_unitofMeasureCode_binding_containerline" Text="{Binding UnitofMeasureCode}" Grid.Row="3" Grid.Column="1" VerticalTextAlignment="Center" Style="{StaticResource StandardLabelStyle}" />
                    <Label x:Name="lbl_kgQuantity_binding_containerline" Text="{Binding KgQuantity}" Grid.Row="3" Grid.Column="2" VerticalTextAlignment="Center" Style="{StaticResource StandardLabelStyle}" />

                    <Label x:Name="lbl_binCode_binding_containerline" Text="{Binding BinCode}" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" VerticalTextAlignment="Center" Style="{StaticResource StandardLabelStyle}" />
                  </Grid>
                </ViewCell.View>
              </ViewCell>
            </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>
      </StackLayout>
    </Frame>

【问题讨论】:

  • “如何在列表视图中获取网格”到底是什么意思?我很困惑。
  • 另外,我使用 而不是使用 。我的意思是我遵循模式 我的网格...
  • 我想自定义“grd_containerline”,所以我必须将网格放入一个网格变量中?

标签: listview gridview xamarin grid xamarin.forms


【解决方案1】:

通常,您无法通过名称访问项目模板内的任何控件。 如果您尝试在后面的代码中访问此控件,则在 ItemTemplate 中为任何控件提供 x:Name 都会给您一个编译器错误,而是在 XAML 中分配 Click 处理程序(或使用命令)。

【讨论】:

    【解决方案2】:

    你可以试试这个。

    Xmal 页面

    <ScrollView x:Name="ModuleScrolLView"
                BackgroundColor="#d3d6db">
        <StackLayout x:Name="ModuleStackLayout" 
                     Orientation="Vertical" 
                     VerticalOptions="FillAndExpand" 
                     Padding="10">
            <Grid x:Name="SegmentGrid"
                  RowSpacing="10"
                  ColumnSpacing="10"
                  VerticalOptions="StartAndExpand">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
            </Grid>
            <WebView Source="{Binding DescriptionHtml}" BackgroundColor="White" />
        </StackLayout>
    </ScrollView>
    

    代码端:

        Task.Run(() => BuildSegmentCards());
    
        private void BuildSegmentCards()
        {
    
            var rowDef = new RowDefinition { Height = 150 };
            var frames = new Collection<Tuple<Frame, int, int>>();
    
            for (var i = 0; i < _viewModel.Segments.Count; i++)
            {
                var column = i % 2;
                var row = i / 2;
                frames.Add(new Tuple<Frame, int, int>(BuildGridElement(_viewModel.Segments[i]), column, row));
            }
    
            // this has to run on the UI thread to prevent UI Locking.
            Device.BeginInvokeOnMainThread(() =>
            {
                SegmentGrid.RowDefinitions.Clear();
                SegmentGrid.Children.Clear();
                SegmentGrid.HeightRequest = 470;
    
                foreach (var frame in frames)
                {
                    if (frame.Item3 % 2 != 0)
                    {
                        SegmentGrid.RowDefinitions.Add(rowDef);
                    }
                    SegmentGrid.Children.Add(frame.Item1, frame.Item2, frame.Item3);
                }
            });
        }
    
        private static Frame BuildGridElement(SegmentViewModel value)
        {
            var numberLabel = new Label { Text = value.Number.ToString(), BackgroundColor = Color.White, TextColor = Color.Aqua.WithLuminosity(0.3f) };
            var titleLable = new Label { Text = value.Title, BackgroundColor = Color.White, TextColor = Color.Black };
    
            var innerLayout = new RelativeLayout();
            innerLayout.Children.Add(numberLabel, Constraint.Constant(0));
            innerLayout.Children.Add(titleLable, Constraint.RelativeToView(numberLabel, (p, s) => numberLabel.Width + 3));
    
            var palleteFrame = new Frame
            {
                BackgroundColor = Color.White,
                Padding = 12,
                Content = innerLayout,
                HasShadow = false,
                VerticalOptions = LayoutOptions.FillAndExpand
            };
    
            palleteFrame.GestureRecognizers.Add(new TapGestureRecognizer((callback) => NavigateToSegment(value)));
    
            return palleteFrame;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      • 1970-01-01
      • 2014-09-28
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多