WPF中combobox显示多列的下拉框
2个回答
展开全部
写combobox的comboboxitem的样式就可以了
<ComboBox>
<ComboBox.Resources>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding 产品编码}"></TextBlock>
<TextBlock Text="{Binding 产品名称}"></TextBlock>
<TextBlock Text="{Binding 产品价格}"></TextBlock>
<TextBlock Text="{Binding 产品等级}"></TextBlock>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ComboBox.Resources>
</ComboBox>
后台绑定combobox的itemsource就可以。
<ComboBox>
<ComboBox.Resources>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding 产品编码}"></TextBlock>
<TextBlock Text="{Binding 产品名称}"></TextBlock>
<TextBlock Text="{Binding 产品价格}"></TextBlock>
<TextBlock Text="{Binding 产品等级}"></TextBlock>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ComboBox.Resources>
</ComboBox>
后台绑定combobox的itemsource就可以。
追问
那我只要Combobox显示产品编码,在加三个Textbox用来显示其他三个属性应该怎么做???
展开全部
具体步骤如下:实例以datatable为数据源,数据源也可以是arraylist或通过add()方法添加
int columnPadding = 5;
private float[] columnWidths = new float[0]; //项宽度
private String[] columnNames = new String[0]; //项名称
private int valueMemberColumnIndex = 0; //valueMember属性列所在的索引
/// <summary>
/// 绑定数据源或更改数据源时发生
/// </summary>
/// <param name="e"></param>
protected override void OnDataSourceChanged(EventArgs e)
{
base.OnDataSourceChanged(e);
InitializeColumns();
}
/// <summary>
/// 设置ValueMember属性发生
/// </summary>
/// <param name="e"></param>
protected override void OnValueMemberChanged(EventArgs e)
{
base.OnValueMemberChanged(e);
InitializeValueMemberColumn();
}
/// <summary>
/// 显示下拉框的时候触发
/// </summary>
/// <param name="e"></param>
protected override void OnDropDown(EventArgs e)
{
base.OnDropDown(e);
this.DropDownWidth = (int)CalculateTotalWidth(); //计算combobox下拉框的总宽度
}
/// <summary>
/// 初始化数据源各列的名称
/// </summary>
private void InitializeColumns()
{
PropertyDescriptorCollection propertyDescriptorCollection = DataManager.GetItemProperties();
columnWidths = new float[propertyDescriptorCollection.Count];
columnNames = new String[propertyDescriptorCollection.Count];
for (int colIndex = 0; colIndex < propertyDescriptorCollection.Count; colIndex++)
{
String name = propertyDescriptorCollection[colIndex].Name;
columnNames[colIndex] = name;
}
}
/// <summary>
/// 返回ValueMember在显示列中的索引位置
/// </summary>
private void InitializeValueMemberColumn()
{
int colIndex = 0;
foreach (String columnName in columnNames)
{
if (String.Compare(columnName, ValueMember, true, CultureInfo.CurrentUICulture) == 0)
{
valueMemberColumnIndex = colIndex;
break;
}
colIndex++;
}
}
/// <summary>
/// 计算combobox下拉框的总宽度
/// </summary>
/// <returns></returns>
private float CalculateTotalWidth()
{
columnPadding = 5;
float totWidth = 0;
foreach (int width in columnWidths)
{
totWidth += (width + columnPadding);
}
//总宽度加上垂直滚动条的宽度
return totWidth + SystemInformation.VerticalScrollBarWidth;
}
int columnPadding = 5;
private float[] columnWidths = new float[0]; //项宽度
private String[] columnNames = new String[0]; //项名称
private int valueMemberColumnIndex = 0; //valueMember属性列所在的索引
/// <summary>
/// 绑定数据源或更改数据源时发生
/// </summary>
/// <param name="e"></param>
protected override void OnDataSourceChanged(EventArgs e)
{
base.OnDataSourceChanged(e);
InitializeColumns();
}
/// <summary>
/// 设置ValueMember属性发生
/// </summary>
/// <param name="e"></param>
protected override void OnValueMemberChanged(EventArgs e)
{
base.OnValueMemberChanged(e);
InitializeValueMemberColumn();
}
/// <summary>
/// 显示下拉框的时候触发
/// </summary>
/// <param name="e"></param>
protected override void OnDropDown(EventArgs e)
{
base.OnDropDown(e);
this.DropDownWidth = (int)CalculateTotalWidth(); //计算combobox下拉框的总宽度
}
/// <summary>
/// 初始化数据源各列的名称
/// </summary>
private void InitializeColumns()
{
PropertyDescriptorCollection propertyDescriptorCollection = DataManager.GetItemProperties();
columnWidths = new float[propertyDescriptorCollection.Count];
columnNames = new String[propertyDescriptorCollection.Count];
for (int colIndex = 0; colIndex < propertyDescriptorCollection.Count; colIndex++)
{
String name = propertyDescriptorCollection[colIndex].Name;
columnNames[colIndex] = name;
}
}
/// <summary>
/// 返回ValueMember在显示列中的索引位置
/// </summary>
private void InitializeValueMemberColumn()
{
int colIndex = 0;
foreach (String columnName in columnNames)
{
if (String.Compare(columnName, ValueMember, true, CultureInfo.CurrentUICulture) == 0)
{
valueMemberColumnIndex = colIndex;
break;
}
colIndex++;
}
}
/// <summary>
/// 计算combobox下拉框的总宽度
/// </summary>
/// <returns></returns>
private float CalculateTotalWidth()
{
columnPadding = 5;
float totWidth = 0;
foreach (int width in columnWidths)
{
totWidth += (width + columnPadding);
}
//总宽度加上垂直滚动条的宽度
return totWidth + SystemInformation.VerticalScrollBarWidth;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询