Xamarin表单:在列表视图中获得控制权

时间:2016-08-24 12:28:16

标签: listview gridview xamarin grid xamarin.forms

如何在此列表视图中获取网格(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>

2 个答案:

答案 0 :(得分:1)

通常,您无法按名称访问项目模板中的任何控件。 在ItemTemplate中给出任何控件x:如果你试图在后面的代码上访问这个控件,Name会给你一个编译器错误,而是在XAML中分配Click处理程序(或使用命令)。

答案 1 :(得分:0)

你可以试试这个。

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;
    }