You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Xamarin的TableView中让Label与PickerView同行显示(C#实现)

How to Place Label and Picker in the Same TableView Row with Xamarin C#

Got it, let's figure out how to get your Label and Picker sitting side-by-side in the same TableView row using Xamarin C#. Since you're building your UI in code (instead of XAML), here's a straightforward way to make this work, building on the code you already have:

The key idea is that each row in a TableView is represented by a ViewCell, and you can set any layout (like your horizontal StackLayout) as the View property of that ViewCell. This lets you combine multiple controls in a single row.

Here's the complete code example, with your existing snippets integrated:

// Create your TableView (customize Intent for styling if needed)
var tableView = new TableView
{
    Intent = TableIntent.Settings,
    Root = new TableRoot()
};

// Create a section to group your row (optional but improves organization)
var settingsSection = new TableSection("Auto Save Configuration");

// Build your horizontal StackLayout to hold Label and Picker
var pickerContainer = new StackLayout
{
    Orientation = StackOrientation.Horizontal,
    Padding = new Thickness(13),
    Spacing = 10 // Adds space between the Label and Picker
};

Label autoSaveLabel = new Label
{
    Text = "Auto Save Timer",
    VerticalOptions = LayoutOptions.Center,
    HorizontalOptions = LayoutOptions.Start
};

Picker autoSavePicker = new Picker
{
    HorizontalOptions = LayoutOptions.FillAndExpand,
    VerticalOptions = LayoutOptions.Center,
    // Add your desired picker options here
    Items = { "1 Minute", "5 Minutes", "10 Minutes", "30 Minutes" }
};

// Add controls to the StackLayout
pickerContainer.Children.Add(autoSaveLabel);
pickerContainer.Children.Add(autoSavePicker);

// Wrap the StackLayout in a ViewCell to use as a TableView row
var autoSaveRow = new ViewCell
{
    View = pickerContainer
};

// Add the row to the section, then the section to the TableView's root
settingsSection.Add(autoSaveRow);
tableView.Root.Add(settingsSection);

// Finally, add the TableView to your page's content (adjust based on your page setup)
Content = tableView;

Quick Key Points:

  • ViewCell as Row Container: Every row in a TableView requires a ViewCell. By assigning your horizontal StackLayout to its View property, you can combine multiple controls in one row.
  • Layout Control: HorizontalOptions = LayoutOptions.FillAndExpand on the Picker ensures it takes up all remaining space in the row, while the Label stays aligned to the left.
  • Styling Tweaks: Adjust Spacing (between controls) and Padding (around the row content) to match your app's design system.

内容的提问来源于stack exchange,提问作者David Kachlon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:12:58