WPF Style Combobox with two columns in the drop-down menu

I looked around, found something, and now I'm stuck in a drop-down list with two columns displayed in a drop-down list. I have xaml themes available, and the combobox "Style" is defined and works well, as expected, so the part is fine.

Now I have a combo box that should display two values, and think of it as a state reduction and state name for the drop-down list coming from the DataTable.DefaultView binding source for the items.

If i have

<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> 

it works. Now, how / where am I stuck ... Now I want the same functionality to say 5 different forms and all, to display the same content, and if ever change (not this, but for other multi-column combobox) I I don’t want it to be directly in the form of a XAML file.

I was hoping to put themes in the dictionary resource file and just keep reusing this “style” over and over again. Has the meaning. However, when I do this, and bind to the data table, the only results I get when I try to do as a style is a drop-down menu showing the values

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

instead of the actual 2 columns. Here's what I have in the dictionary of the words "topic."

 <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> 

So, if I have TWO instances, my class “cboStates” created on the form set one to the explicit style specified by the first, and SECOND based on the “Style” setting, the second failed, only showing duplicate System.Data. DataRowView, not the actual contents of the data.

What am I missing.

So, to clarify what I'm looking for ... States ... ex data p>

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

I want the combo box to display the abbreviated AL, AK, AZ, etc. And a narrower combo box. It will also be "SelectedValue" upon return.

The actual drop-down menu will contain the data listed above showing BOTH abbreviation AND a long description of the status.

Example of the desired character

enter image description here

+6
source share
1 answer

FINALLY got his job ... and for those who try this way. Since I was trying to create a standard instance of the class that could be used everywhere, but not wanting to explicitly reference the XAML on each page, part of the style had to be handled during the actual instance of the class inside the code.

Since I don’t know exactly how / when all its controls, style settings, etc. are built in the .NET Framework, I was upset that it would work if it was directly from xaml, but did not work in the code. So I ended up formatting the values ​​of the elements and TextSearch.TextPath values ​​in the code. Here's a short snippet of class

 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" ); } } 

Now, a special note. In the routine I used to populate the DataTable, I pre-check if the table exists or not. The first time I create a table. If I need to refill it, if I just keep doing the “new DataTable” every time, it resets the data / element template bindings. To PREVENT this, I would then do

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

Then i call

Sqlexecute call request from database (DataAdapter) and Fill () for datatable.

So, now everything seems to be filled out correctly, the bindings for display and text search are complete and ready to go.

0
source

All Articles