c# - ListView 内的组合框绑定(bind) WPF

标签 c# wpf xaml combobox

把我的头发拉出来。我无法将 listview 中的 combobox 绑定(bind)到后面代码中的列表。

此外,组合框甚至没有出现在列中..

想要 listview 中的组合框显示数字 0-24。

XAML:

<ListView Grid.Row="0" Margin="0,0,0,0" Height="250" Width="540" SelectionMode="Single" dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True" x:Name="TasksList">
    <ListView.View>
        <GridView>
            <GridViewColumn Header ="Day 1" Width="50">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding Path=ComboBox1}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
       </GridView>
    </ListView.View>
</ListView>

以及背后的代码:

public partial class TaskHoursRemaining : Page {

    List<int> hourOfDay = new List<int>();

    public TaskHoursRemaining() {
        InitializeComponent();
        LoadData();
        DataContext = this;
    }

    private void LoadData() {
        for (int i = 0; i < 25; i++) {
            hourOfDay.Add(i);
        }
        this.ComboBox1.ItemsSource= hourOfDay;
    }
}

但是当前上下文中不存在ComboBox1

最佳答案

在您的 XAML 中,您绑定(bind)到一个不存在的属性 ComboBox1:

<ComboBox ItemsSource="{Binding Path=ComboBox1}"/>

在您的代码隐藏中,您正在访问一个不存在的字段ComboBox1:

this.ComboBox1.ItemsSource= hourOfDay;

DataContext = this; 语句在这里对您没有任何用处。

要通过 XAML 创建字段,您应该使用 x:Name 属性。无论如何,这对您没有帮助,因为 ComboBox 驻留在模板中。

@un-lucky 是正确的,您应该将 ListView 绑定(bind)到集合(这实际上是您在代码隐藏中尝试执行的操作)。话又说回来,组合框需要一个集合,因此您应该正确地拥有一个由集合组成的数据模型。 (有点——所有组合框都需要相同的集合;只有所选项目会有所不同。)

让我们首先使用 TextBox 而不是 ComboBox 来实现此操作。该列表绑定(bind)到 hourOfDay,而 TextBox 显示 int:

private readonly List<int> hourOfDay = new List<int>();

public MainWindow()
{
    InitializeComponent();

    for (int i = 0; i < 25; i++)
    {
        this.hourOfDay.Add(i);
    }

    this.TasksList.ItemsSource = this.hourOfDay;
}

XAML:

<ListView Grid.Row="0" Margin="0,0,0,0" Height="250" Width="540" SelectionMode="Single" x:Name="TasksList">
    <ListView.View>
        <GridView>
            <GridViewColumn Header ="Day 1" Width="50">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Mode=OneWay}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

结果:

enter image description here

不过,您想要的是一个内容列表,其中每个组合框都有一个包含 1-24 的下拉列表。我不知道可能是什么——也许是这样的:

public class Entry
{
    private static readonly List<int> hourOfDay;

    static Entry()
    {
        hourOfDay = new List<int>();
        for (int i = 0; i < 25; i++)
        {
            hourOfDay.Add(i);
        }
    }

    public IEnumerable<int> HourOfDaySource => hourOfDay;
}

在窗口/页面构造函数中:

InitializeComponent();
this.TasksList.ItemsSource = new List<Entry>
    {
        new Entry(),
        new Entry(),
        new Entry(),
        new Entry(),
        new Entry(),
    };

XAML:

<ListView Grid.Row="0" Margin="0,0,0,0" Height="250" Width="540" SelectionMode="Single" x:Name="TasksList">
    <ListView.View>
        <GridView>
            <GridViewColumn Header ="Day 1" Width="60">
                <GridViewColumn.CellTemplate>
                    <DataTemplate DataType="wpf:Entry">
                        <ComboBox
                            ItemsSource="{Binding HourOfDaySource, Mode=OneWay}"
                            SelectedIndex="12"
                            Width="42"
                        />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

结果:

enter image description here

要使其发挥作用,需要大量的管道,但至少您已经填充了 ComboBoxes...

关于c# - ListView 内的组合框绑定(bind) WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40878312/

相关文章:

c# - 如何在 C# 中比较两个 .wav 文件

c# - 文本框的实时绑定(bind)属性更改

c# - EF 6 中缺少 QueryableExtensions

c# - 从 Source 获取图片 uri

c# - C#任务-从UI异步获取值(value)

c# - 如何使用 C#/LINQ 计算加权平均值

wpf - 以编程方式更改标签属性

c# - 向图像添加行为

c# - LoadComponent() 与。 XamlReader.Load()?

c# - 如何使用 C# 启动之前停止的 Azure 容器实例?