将值绑定到ComboBox WPF

时间:2019-01-06 17:04:16

标签: c# wpf

因此,我需要制作一个WPF应用程序,该应用程序将从列表中获取值并将其存储到DataGrid。我想在网格中显示3个字段:用户名,全名和角色。现在,角色可以具有2个值:购物者和供应商。如何使该部分成为DataGridTemplate?此外,如何为每个用户选择合适的负载?空行是缺少代码的位置。这是我正在处理的代码:

<Grid>
    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding UserList}" Name="dgUsers" HorizontalAlignment="Left" Height="450" Width="400">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Username" Width="100" IsReadOnly="True">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Username}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Full Name" Width="200" IsReadOnly="True">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding FullName}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Role" Width="100">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>

                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

编辑:

作为数据模板,我这样做:

<DataGridTemplateColumn.CellTemplate>
     <DataTemplate>
            <ComboBox ItemsSource="{Binding valueList}" SelectedIndex="{Binding Role}"/>
     </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

作为角色,在构造函数中,我这样做:

public class UserToChange
{
    public string Username { get; set; }
    public string FullName { get; set; }
    public int Role { get; set; }
    public List<string> valueList { get; set; }

    public UserToChange()
    {

    }

    public UserToChange(UserVM row)
    {
        Username = row.UserName;
        FullName = row.FirstName + " " + row.LastName;
        Role = row.RoleId == 102 ? 0 : 1;
        valueList = new List<string>
        {
            "Supplier",
            "Shopper"
        };
    }
}

它奏效了。现在,我需要对逻辑进行编程以更新数据库,但是我可以解决这个问题。谢谢大家!

2 个答案:

答案 0 :(得分:0)

您可以通过两种方式绑定集合。

1)在ComboBox内创建一个DataTemplate

         <DataGridTemplateColumn Header="Role" Width="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding dataList}"  Width="100" DisplayMemberPath="{Binding DisplayProperty}" SelectedValuePath="{Binding PropertyId}" SelectedIndex="0" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

对于ComboBox,使用SelectedIndex设置默认的加载项。

2)代替DataGridTemplateColumn使用DataGridComboBoxColumn

<DataGridComboBoxColumn ItemsSource="{Binding dataList}" Width="100" Header="Role2" DisplayMemberPath="{Binding DisplayProperty}" SelectedValuePath="{Binding PropertyId}" SelectedItemBinding="{Binding DefaultValuePro, Mode=TwoWay}" />

对于DataGridComboBoxColumn,请使用SelectedItemBinding将其与属性绑定以设置默认值。

答案 1 :(得分:0)

  1. 首先用您各自的值填充ListofRole集合。
  2. 使用DataGridTemplateColumn内的组合框将其绑定。
  3. 绑定组合框的SelectedValue属性,神奇的是,将SelectedValue绑定到在dataList中包含Role值的变量。

    注意:ListofRole集合不应该是自定义类型,那么我们需要在组合框中添加更多参数,它应该是普通的字符串类型。

             <DataGridTemplateColumn Header="Role" Width="0.45*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding ListOfRole}" 
                                      SelectedValue="{Binding Role,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
    
相关问题