![](/img/trans.png)
[英]WPF Binding TextBlock value to display SelectedItem in ComboBox
[英]Binding value to ComboBox 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>
编辑:
作为DataTemplate,我这样做:
<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"
};
}
}
而且有效。 现在,我需要对逻辑进行编程以更新数据库,但是我可以解决这个问题。 感谢大家!
您可以通过两种方式绑定Collection。
1)在DataTemplate
创建一个ComboBox
<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将其与属性绑定以设置默认值。
绑定组合框的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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.