开发者

Bind SelectedValue to a WPF combobox

I've tried searching up and down for this but can't find anything. I have a combobox in a listview. The listview is bound to a list of objects exposed through the controller that the datacontext is bound to. One of the properties of the items in the list is a string. I'm trying to bind that value to what's in the combobox.

Here is a snippet of my listview

<ListView ItemsSource="{Binding Path=OrderLines}" >

            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Item Type" Width="Auto">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox Width="100" SelectedItem="{Binding Path=LineType,ValidatesOnDataErrors=True}" >

                                    <ComboBoxItem>Type1</ComboBoxItem>
                                  开发者_开发问答  <ComboBoxItem>Type2</ComboBoxItem>
                                    <ComboBoxItem>Type3</ComboBoxItem>
                                    <ComboBoxItem>Type4</ComboBoxItem>
                                </ComboBox>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>    
                </GridView>
             </ListView.View>

In the controller I have a property called OrderLines like such

    private List<OrderLine> orderLines;
    public List<OrderLine> OrderLines 
    { 
        get { return orderLines; }
        set
        {
            if (value == orderLines)
                return;

            orderLines= value;

            OnPropertyChanged("OrderLines");
        }
    }

And an OrderLine just has a property called LineType that is a string that contains the value.

    private string lineType;
    public string LineType 
    {
        get { return lineType; }

        set
        {
            lineType= value;
            OnPropertyChanged("LineType ");
        }
    }

Can anyone help explain why the selected item/value is not being set. Does it have something to do with my content being hardcoded? Thanks for your help.


It probably doesn't bind because LineType is a string and ComboBox contains ComboBoxItems, and a string != ComboBoxItem.

Try something along

 <ComboBox>
      <system:String>Item1</system:String>
      <system:String>Item2</system:String>
 </ComboBox>

where system is a namespace referencing System in mscorlib

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜