WPF Style Combobox下拉列表中有两列

时间:2012-12-20 19:53:58

标签: c# wpf combobox styles multiple-columns

我环顾四周,找到了一些东西,现在卡在一个组合框中,下拉区域显示两列。我有一个xaml主题可用,并且组合框“样式”已定义并且在预期中运行良好,因此该部分是可以的。

现在,我有一个组合框,我需要显示两个值,将其视为状态缩写和下拉列表的状态名称,来自项目的DataTable.DefaultView绑定源。

如果我有

<my:cboStates TextSearch.TextPath="StateAbbrev">
  <ComboBox.ItemTemplate>
    <DataTemplate>
      <StackPanel Orientation="Horizontal" TextSearch.Text="{Binding Path=StateAbbrev}">
        <TextBlock Text="{Binding Path=StateAbbrev}"/>
        <TextBlock Text="{Binding Path=FullStateName}" Margin="10 0"/>
      </StackPanel>
    </DataTemplate>
  </ComboBox.ItemTemplate>
</my:cboStates>
这是有效的。现在,我被困在哪里/哪里...现在,我希望在5个不同的表格上显示相同的功能,并且所有内容都显示相同的内容,如果有的话(不是这个,但是对于其他多列组合框),我不想将其直接放在表单的XAML文件中。

我希望将主题的资源字典文件放入其中,并且不断重复使用“风格”。说得通。但是,当我这样做,并且绑定到数据表时,我尝试作为样式时获得的唯一结果是下拉列表显示值

System.Data.DataRowView
System.Data.DataRowView
System.Data.DataRowView
System.Data.DataRowView

而不是实际的2列。 这是我在“主题”资源字典中的内容。

<DataTemplate x:Key="myStateComboTemplate" >
  <StackPanel Orientation="Horizontal"  >
    <TextBlock Text="{Binding Path=StateAbbrev}"/>
    <TextBlock Text="{Binding Path=FullStateName}"/>
  </StackPanel>
</DataTemplate>

<Style x:Key="StyleMyStatesCombobox" TargetType="{x:Type ComboBox}" 
  BasedOn="{StaticResource MyOtherWorkingComboBoxStyle}" >
  <Setter Property="TextSearch.TextPath" Value="{Binding Path=StateAbbrev}" />
  <Setter Property="ItemTemplate" Value="{StaticResource myStateComboTemplate}" />
</Style>

所以,如果我有两个实例,我在表单上创建了“cboStates”类,并将其中一个设置为首先列出的显式样式,而将SECOND设置为基于“样式”设置,则第二个实例仅显示重复System.Data.DataRowView条目,而不是实际的数据内容。

我错过了什么。

所以,澄清一下我在寻找什么...... 国家......前数据

AL Alabama
AK Alaska
AZ Arizona
AR Arkansas
CA California
CO Colorado
CT Connecticut
DE Delaware

我希望组合框显示缩写 AL,AK,AZ等和更窄的组合框。返回时,这也将是“SelectedValue”。

实际下拉列表将显示上面列出的数据,显示缩写和状态的长描述。

所需组合框的样本

enter image description here

1 个答案:

答案 0 :(得分:0)

最终让它发挥作用......对于那些尝试相似的人。由于我试图拥有一个可以在整个过程中使用的标准“类”实例,但又不想在每个页面中明确地硬引用XAML,因此必须在实际的代码内类实例中处理部分样式。 / p>

由于我不确切知道.net框架在何处/何时构建其所有控件,样式分配等,我感到沮丧的是,如果直接来自xaml它会工作,但在代码中失败。所以,我最终在代码中强制执行项模板和TextSearch.TextPath值。这是课程的简短片段

public class myStatesCombo : ComboBox
{
   public myStatesCombo()
   {
      Loaded += myAfterLoaded;
   }

   protected static DataTable myTableOfStates;

   public void myAfterLoaded()
   {
      if( myTableOfStates == null )
        myTableOfStates = new DataTable();

      CallProcedureToPopulateStates( myTableOfStates );

      ItemsSource = myTableOfStates.DefaultView;

      // AFTER the object is created, and all default styles attempted to be set,
      // FORCE looking for the resource of the "DataTemplate" in the themes.xaml file
      object tryFindObj = TryFindResource("myStateComboTemplate" );
      if( tryFindObj is DataTemplate )
         ItemTemplate = (DataTemplate)tryFindObj;

      // NOW, the CRITICAL component missed in the source code
      TextSearch.SetTextPath( this, "StateAbbrev" );
   }
}

现在,特别说明。在我用来填充DataTable的例程中,我预先检查表是否存在。第一次,我创建了表。如果我需要重新填充它,如果我每次只是继续做一个“新的DataTable”,它会吹掉数据/项目模板绑定。为了防止这种情况,我会做

if( myTableOfStates.Rows.Count > 0 )
  myTableOfStates.Rows.Clear();

然后,我打电话给我

Sqlexecute从数据库调用查询(DataAdapter)和Fill()数据表。

因此,现在所有内容都显示正确填充,显示和文本搜索的绑定已完成并准备就绪。