开发者

WPF DataGrid - Problem Customizing a ComboBox

I have a Datagrid with a combo box bound column that works great as follows:

<tk:DataGridComboBoxColumn SelectedValueBinding="{Binding DefaultLocationID}"                                            
    SelectedValuePath="LocationID"
    DisplayMemberPath="LocationName"    
    Header="Default Location"
    ItemsSource="{Binding Source={StaticResource CustomerLocations}}">
</tk:DataGridComboBoxColumn>

Ultimately I want to customize the dropdown to show 'ID' and 'Name' together so a TemplateColumn seems to be the way to go. Starting small, I can't get the following simple example to work which should replace the standard DataGridComboBoxColumn. The dropdown appears fine, but when I 'select' a dropdown item, it does not accept and goes back to the previous value. I've tried variations on 'Mode=Tw开发者_如何学编程oWay' but no luck. Do I need a 'CellEditingTemplate' ?

DefaultLocationID is the foreign-key field being edited, while 'LocationID' is a column in 'CustomerLocations'.

<tk:DataGridTemplateColumn  Header="Default Location">
    <tk:DataGridTemplateColumn.CellTemplate>
       <DataTemplate>
            <ComboBox   VerticalAlignment="Top"   
                  SelectedItem="{Binding LocationID, Mode=TwoWay}" 
                  SelectedValue="{Binding DefaultLocationID, Mode=TwoWay}"  

                  ItemsSource="{Binding Source={StaticResource CustomerLocations}}"
                        SelectedValuePath="LocationID"
                        DisplayMemberPath="LocationName" >
              </ComboBox>
       </DataTemplate>
  </tk:DataGridTemplateColumn.CellTemplate>

Thanks!!


Can you post the relevant parts of your CustomerLocations resource? Can you also post the type of class that your grid is binding to?

Try removing the SelectedValuePath, DisplayMemberPath and SelectedValue from the ComboBox.

If you want to display multiple pieces of data in your combobox see the below XAML

<ComboBox ...>
    <ComboBox.ItemTemplate>
        <DataTemplate>
             <StackPanel Orientation="Horizontal">
                  <TextBlock Text="{Binding LocationId}"} />
                  <TextBlock Text=" - "/>
                  <TextBlock Text="{Binding LocationName}"} />
             </StackPanel>
       </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜