【问题标题】:UWP ComboBox in ListView to edit tableListView 中的 UWP ComboBox 以编辑表格
【发布时间】:2017-04-06 18:59:34
【问题描述】:

在开发 UWP C# 应用方面,我完全是个新手。但是,出于兴趣,我打算建立一个小项目。

目前我正在努力建立一个基于 sqlite 数据库的表。我的具体问题是我想使用组合框编辑表格的一列。

为了说明我的问题,我创建了一个小示例来解释我正在努力解决的问题:

我的数据绑定使用自定义 ListView,并且使用 ObservableCollection 按预期工作。

Sample 类和 ObservableCollection 的设置都在 Mainpage.cs 中,如下:

public sealed partial class MainPage : Page
    {
        public ObservableCollection<Car> Cars;
        public MainPage()
        {
            this.InitializeComponent();
            Cars = new ObservableCollection<Car>();
            Cars.Add(new Car { Brand = "BMW", Colour = { "red", "yellow" } });
            Cars.Add(new Car { Brand = "Mercedes", Colour = { "blue", "yellow" } });
            Cars.Add(new Car { Brand = "Scoda", Colour = { "green", "pruple" } });
        }

        private void MyComboBox_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            var myComboBox = (ComboBox)sender;
            myComboBox.ItemsSource = Cars;
        }
    }
    public class Car
    {
        public string Brand { get; set; }
        public List<string> Colour { get; set; }

        public Car()
        {
            Colour = new List<string>();
        }

    }

MainPage.xaml 中 ListView 的 MyDatabinding 如下所示:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <!--header of table-->
        <Grid Background="{ThemeResource GridHeaderBackgroundBrush}"  Margin="40,40,40,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="3*" />
                <ColumnDefinition Width="3*" />
            </Grid.ColumnDefinitions>
            <TextBlock Text="Brand" 
                       Grid.Column="0" 
                       Style="{StaticResource GridHeadingTextBlockStyle}"/>
            <TextBlock Text="Colour" 
                       Grid.Column="1" 
                       Style="{StaticResource GridHeadingTextBlockStyle}"/>
        </Grid>
        <controls:AlternatingRowListView
             Margin="40,0,40,0"
            ItemsSource="{x:Bind Cars}"
            OddRowBackground="{ThemeResource GridOddRowBackgroundBrush}" EvenRowBackground="{ThemeResource GridEvenRowBackgroundBrush}"
			ItemContainerStyle="{StaticResource ListViewItemStyle}"
			ScrollViewer.HorizontalScrollMode="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto"
			Grid.Row="1"
            x:Name="myListView">
            <controls:AlternatingRowListView.ItemTemplate>
                <DataTemplate x:DataType="data:Car">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="3*" />
                            <ColumnDefinition Width="3*" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{x:Bind Brand}" Grid.Column="0" />
                        <ComboBox
                            x:Name="MyComboBox"
                            Grid.Column="1"
                            Loaded="MyComboBox_Loaded"
                            SelectedValuePath="Colour"
                            DisplayMemberPath="Colour">
                        </ComboBox>
                    </Grid>
                </DataTemplate>
            </controls:AlternatingRowListView.ItemTemplate>
        </controls:AlternatingRowListView>
    </Grid>

仅供参考:请注意,我在 Load 事件中为 Combobox 的 ItemsSource 进行了绑定,因为我没有找到不同的解决方案。我不确定是否有办法从listview的上层数据模板继承ItemsSource。

执行的 UI 如下所示: enter image description here

我的问题是颜色的值没有正确显示。由于某种原因,我只是得到 System.Collections.Generic.List'1 [System.String]。

如果有人能给我提示,我会很高兴...

【问题讨论】:

    标签: listview combobox uwp


    【解决方案1】:

    您的主要问题在于 ComboBox 的绑定。您需要在 XAML 中添加以下绑定并删除 Loaded 事件处理程序。

    <ComboBox
        x:Name="MyComboBox"
        Grid.Column="1"
        ItemsSource="{x:Bind Colour}"
        SelectedItem="{x:Bind CurrentColour, Mode=TwoWay}" >
    </ComboBox>
    

    之所以有效,是因为 Color 是一个字符串列表,因此它们可以正确显示。如果它是一个复杂的对象,您可以使用 DisplayMemberPath 来获取特定的显示字符串。

    您将代码中的 ComboBox 绑定到 Cars(错误的对象),然后使用显示路径,但这只会让您获得字符串列表,而不是颜色本身。

    我不确定这是否正是您所追求的。我假设您接下来想要将所选颜色保存在 ComboBox 中,因此您需要充实您的 Car 类以包含 ColourList 和 SelectedColour。

    【讨论】:

    • 谢谢。这行得通。你知道如何在 xaml 中设置 selectedindex 吗?
    • 酷,如果这对你有用,你能接受我的回答吗?对于 CurrentColour,您需要 Car 上的另一个属性。然后将 ComboBox.SelectedItem 绑定到它:SelectedItem="{x:Bind CurrentColour, Mode=TwoWay}"。
    • 非常有帮助 ;)
    • @Paul 很高兴这对你也有用。如果解决了您的问题,您能否接受答案?谢谢。
    • @Paul,如果您不知道如何接受答案,请阅读What should I do when someone answers my question?。接受一个能很好地回答你的问题的答案。
    猜你喜欢
    • 1970-01-01
    • 2016-10-30
    • 2020-10-17
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 2016-05-03
    • 2012-11-25
    • 1970-01-01
    相关资源
    最近更新 更多